php function code that automatically adds keyword links to articles

  • 2020-05-26 08:04:39
  • OfStack

 
<?php 
$link = array( 
' baidu ,http://www.baidu.com/', 
' Server software ,http://s.ofstack.com/', 
' The home of the script ,https://www.ofstack.com/', 
); 
$str = ' In baidu search server software can be provided to the script house software <br /> 
 No longer wearing stockings in summer has recently become a trend 1 Fashion, even in the fall. <br /> 
 In fact, this fashion is not good for legs, experts claim. It is reported, sit for a long time to stand to lack the reason such as motion to bring about varicose vein easily, especially the person that stands for a long time to work, easy cause vein of lower limbs itself dilate, lengthen or vein valve is damaged come on. Specific expression is leg ministry is swollen, vein of lower limbs resembles earthworm to bend or nodule becomes a mass, the skin is purple, especially ankle and crus inside more serious. <br /> 
 Experts point out that people who stand for long periods of time have more or less all of them 1 Some lower extremity varicose vein performance, just severity degree is not 1 The sample. Varicose veins of the lower extremities 1 The disease that kind treats rises to be troublesome quite, serious need is operated on, so the best method still is preventive in the first place, when the symptom is not apparent, take precautionary measure, wear long tube stretch socks is 1 A good way to get blood into larger, deeper veins. <br /> 
 Maintaining an ideal weight can reduce the chance of varicose veins; Avoid tight clothing and wear stretch socks to avoid blood buildup in the legs. Be careful with birth control pills. Some birth control pills can cause this problem. '; 
$out=keylink($str,$link,1); //$str  The original character  $link , replace the linked array , 3 Replace number  
echo $out; 
function _sortDesc($a, $b) { 
return (strlen($a[0]) < strlen($b[0])) ? 1 : -1; 
} 
function keylink($str,$link,$count=1) 
{ 
$linkDefs = $link; 
$linkMap = array(); 
foreach($linkDefs as $row) { 
$linkMap[] = explode(',', $row); 
} 

foreach($linkMap as $row) { 
$str = preg_replace('/(<a.*?>\s*)('.$row[0].')(\s*<\/a>)/sui', '${2}', $str); 
} 

usort($linkMap, '_sortDesc'); 

$tmpKwds = array(); 

foreach($linkMap as $i=>$row) { 
list($kwd, $url) = $row; 
for($j=$i+1; $j<count($linkMap); $j++) { 
$subKwd = $linkMap[$j][0]; 
// If other keywords are included, temporarily replace them with other strings  
if(strpos($kwd, $subKwd) !== false) { 
$tmpKwd = '{'.md5($subKwd).'}'; 
$kwd = str_replace($subKwd, $tmpKwd, $kwd); 
$tmpKwds[$tmpKwd] = $subKwd; 
} 
} 
// Replace text with links  
$str = preg_replace('/('.$row[0].')/sui', '<a href="'.$row[1].'" target="_blank">'.$kwd.'</a>', $str, $count); 
} 

// Replace the string that replaced the subkeyword  
foreach($tmpKwds as $tmp=>$kwd) { 
$str = str_replace($tmp, $kwd, $str); 
} 
return $str; 
} 
?> 

Related articles: