/**
- * Content Keyword Replacement
- * by bbs.it-home.org
- */
$arr =array (
- "Shoes" = "shoes",
- "Socks" = "socks",
- "Skirt" = "skirt",
- "Dresses" and "dresses",
- );
- $str = "Shoes socks shoes sock shoe socks shoes socks shoes socks skirt dress";
- $str =replacelink ($str, $arr, 3);
- Echo $str;
Public Function Replacelink ($STR, $arr, $maxcount =3000) {
- Match a picture
- Preg_match_all ("/(]*>)/ius", $str, $imgs);
- Preg_match_all ("/(.*<\/a>)/ius", $str, $links);
- Replace pictures and links
- if ($links [1]) {
- foreach ($links [1] as $k = + $v) {
- $str =str_replace ($v, "@links_ $k", $str);
- }
- }
- if ($imgs [1]) {
- foreach ($imgs [1] as $k = + $v) {
- $str =str_replace ($v, "@imgs_ $k", $str);
- }
- }
- Match end
- $str =strtr ($str, $arr);
- Preg_match_all ("/(.*<\/a>)/ius", $str, $a);
- $a = $a [1];
- $c =array_count_values ($a);
- if ($c) {
- $KK = 0;
- foreach ($c as $k = = $v) {
- if ($v) {
- if ($kk < $maxcount) {
- $v = $v-1;
- }
- $str =preg_replace ("/". $this->replace_quote ($k). " /i ", Str_replace (" # "," "", Strip_tags ($k)), $str, $v);
- }
- $kk + +;
- }
- }
- Restore pictures and Links
- if ($links [1]) {
- foreach ($links [1] as $k = + $v) {
- $str =str_replace ("@links_ $k", $v, $STR);
- }
- }
- if ($imgs [1]) {
- foreach ($imgs [1] as $k = + $v) {
- $str =str_replace ("@imgs_ $k", $v, $STR);
- }
- }
- return $str;
- }
function Replace_quote ($STR) {
- $str =preg_quote ($STR);
- $str =str_replace ("/", "\ \", $STR);
- return $str;
- }
Copy Code |