In order to optimize internal links, we need to add key links to the content. If we add affiliate links to the content, how to add it?
Method 1: Manually edit and add
Method 2: Use a program to output content without affecting the editing of background content.
The following is a method and example of PHP automatically adding affiliate links to article content:
Keylinks function method:
Parameter 1: Content to be processed
Parameter 2: Number of substitutions
Return result: processed content
Header("Content-Type:text/html;charset=utf-8"); //Set encoding
$linkdatas=array(
array('front-end development', 'http://blog.kgula.com'),
array('front-end design', 'http://blog.kgula.com'),
array('Web front-end', 'http://blog.kgula.com'),
array('front-end blog', 'http://blog.kgula.com'),
);
echo "Before replacing
";
echo $str='Web front-end development - focusing 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[] = '/(?!(
$word2[] = $v[0];
$replacement[] = '' . $v[0] . '
}
if ($replacenum != '') {
$txt = preg_replace($word1, $replacement, $txt, $replacenum);
} else {
$txt = str_replace($word2, $replacement, $txt);
}
}
return $txt;
}