The
Title does not know if it is appropriate. The specific situation is this: the site to increase the keyword link function, and then need to make a regular expression of the content of the article to match and replace, and then use the Preg_replace function. The replacement program code is as follows:
function Replacekeyword ($linkDefs, $content) {$linkMap = array (); /*foreach ($linkDefs as $row) {$linkMap [] = Explode (', ', $row);}
* * $linkMap = $linkDefs; Replace the original chain replacement with a text foreach ($linkMap as $row) {$content = Preg_replace (<a.*?>\s*) ('. $row [0]. ')
(\s*<\/a>)/sui ', $row [0], $content);
}//keyword from long to short sort usort ($linkMap, ' _sortdesc ');
Var_dump ($LINKMAP); $tmpKwds = Array ();
Storing the temporarily replaced sub-keyword $k _count=0; foreach ($linkMap as $i => $row) {list ($KWD, $url) = $row; for ($j = $i +1; $j <count ($LINKMAP); $j + +) {$subKwd = $linkMap [$j]
[0]; If the other keywords are included, temporarily replace them with other strings, such as if the tea becomes if (Strpos ($KWD, $SUBKWD)!== false) {$tmpKwd = '; $kwd = Str_replace ($SUBKWD, $TMPKWD, $
KWD);
$tmpKwds [$TMPKWD] = $SUBKWD; }//Replace text with link require (mleinc. ')
/config/globals.config.php '); $th _num = $config [' Keyword_num ']; Number of keyword substitutions $content = preg_replace ('/(') $row [0]. ') /sui ', ' <a href= '. $row [1]. ' " > '. $kwd. ' </a> ', $content, $th _num, $count);
All matches will be replaced $k _count+= $count; ///Replace the string in place of the sub-keyword back to foreach ($tmpKwDS as $tmp => $kwd) {$content = Str_replace ($tmp, $KWD, $content);} $result = Array ($content, $k _count);
return $result;
Unset ($result);
Unset ($TMP);
Unset ($tmpKwds);
Unset ($KWD);
Unset ($count);
Unset ($config);
Unset ($LINKMAP);
Unset ($linkDefs);
Unset ($TMPKWD);
Unset ($content);
unset ($th _num);
Unset ($row);
unset ($k _count); }
Program is found from the Internet, and then the local test is normal, the local environment for PHP 5.3 service is 5.2, uploaded to the Internet after the submission is displayed blank, a start to consider is the PHP version of the problem, thought is ereg preg difference, after the replacement or not. Later on the Internet to see, found that a netizen said to adjust the big pcre.backtrack_limit and Pcre.recursion_limit on the line, I tried, sure enough. Seems to be a configuration problem, but in general, the default configuration of PHP should be no problem, I wrote this program is not good enough!