PHP association link common code

  • 2020-05-26 07:56:42
  • OfStack

Method 1: manually edit the add
Method 2: use the program to output content without affecting the editing of background content.

Here's how PHP automatically adds associative link methods and examples to article content:

Keylinks function method:
Parameter 1: what to process
Parameter 2: number of substitutions
Returns the result: the processed content

 
header("Content-Type:text/html;charset=utf-8"); // Set the coding  
$linkdatas=array( 
array(' The front-end development ', 'http://https://www.ofstack.com'), 
array(' The front end design ', 'https://www.ofstack.com'), 
array('Web The front end ', 'https://www.ofstack.com'), 
array(' The front-end blog ', 'https://www.ofstack.com'), 
); 
echo " Replace the former <br/>"; 
echo $str='Web The front-end development  -  Focus on website front-end design and Web User experience. Front-end development, focus Web Front-end development, attention Web User experience, focus on the latest and best front-end design resources and front-end development technology professional front-end blog '; 
echo "<br/> After the replacement <br/>"; 
echo $str=keylinks($str,2); 
/** 
*  Associated keyword substitution  
* @param txt $string  The original string  
* @param replacenum $int  Replace number  
* @return string  Return string  
*/ 
function keylinks($txt, $replacenum = '') { 
global $linkdatas; 
if ($linkdatas) { 
$word = $replacement = array(); 
foreach ($linkdatas as $v) { 
$word1[] = '/(?!(<a.*?))' . preg_quote($v[0], '/') . '(?!.*<\/a>)/s'; 
$word2[] = $v[0]; 
$replacement[] = '<a href="' . $v[1] . '" target="_blank" class="keylink">' . $v[0] . '</a>'; 
} 
if ($replacenum != '') { 
$txt = preg_replace($word1, $replacement, $txt, $replacenum); 
} else { 
$txt = str_replace($word2, $replacement, $txt); 
} 
} 
return $txt; 
} 

Related articles: