header("Content- Type:text/html;charset=utf-8"); //Set encoding
$linkdatas=array(
array('front-end development', 'http://http://www.jb51.net '),
array('Front-end design', 'http://www.jb51.net'),
array('Web front-end', 'http://www.jb51.net'),
array('front-end blog', 'http://www.jb51.net'),
);
echo "before replacement
";
echo $str=' Web front-end development-focuses on website front-end design and Web user experience. Front-end development, a professional front-end blog focusing on Web front-end development, focusing on Web user experience, and focusing on the latest and best front-end design resources and front-end development technologies at home and abroad';
echo "
After replacement
";
echo $str=keylinks($str,2);
/**
* Related keyword replacement
* @param txt $string original string
* @param replacenum $int number of replacements
* @return string return string
*/
function keylinks($txt, $replacenum = '') {
global $linkdatas;
if ($linkdatas) {
$word = $replacement = array();
foreach ($linkdatas as $v) {
$word1[] = '/(?! (
)/s';
$word2[] = $v [0];
$replacement[] = '' . $v[0] . ' ';
}
if ($replacenum != '') {
$txt = preg_replace($word1, $replacement, $txt, $replacenum);
} else {
$txt = str_replace($word2, $replacement, $txt);
}
}
return $txt;
}