Home  >  Article  >  Backend Development  >  PHP code to automatically add keyword links to articles

PHP code to automatically add keyword links to articles

WBOY
WBOYOriginal
2016-07-25 09:10:11899browse
  1. $link = array(

  2. 'Baidu,http://www.baidu.com/',
  3. 'Server software,http://s.jbxue.com /',
  4. 'Script Home, http://bbs.it-home.org/',
  5. );
  6. $str = 'Search for server software in Baidu to find the software provided by Script Home
  7. No longer wearing stockings in summer has become a fashion in recent years, even in autumn.
  8. Experts point out that this fashion is actually not conducive to beautiful legs. It is reported that sitting and standing for long periods of time, lack of exercise and other reasons can easily lead to varicose veins. Especially for people who stand and work for a long time, it is easy to cause the veins of the lower limbs to expand and lengthen, or the vein valves to be damaged and even cause disease. The specific manifestations include swelling of the legs, veins in the lower limbs bending like earthworms or forming nodules, and the skin becomes purple, especially on the inner side of the ankles and calves.
  9. Experts point out that people who stand and work for a long time have more or less varicose veins in the lower limbs, but the severity varies. Varicose veins of the lower limbs are a troublesome disease to treat, and severe cases require surgery. Therefore, the best way is to nip it in the bud. When the symptoms are not obvious, take preventive measures. Wearing long elastic stockings is one way. Great way to help get blood into larger, deeper veins.
  10. Maintaining an ideal weight can reduce the chance of varicose veins; avoid tight clothing and wear medical elastic stockings to avoid blood accumulation in the legs; be careful when taking birth control pills, as some birth control pills may cause this problem. ';
  11. $out=keylink($str,$link,1); //$str original character $link, replacement link array, 3 replacement times
  12. echo $out;
  13. function _sortDesc($a, $b) {
  14. return (strlen($a[0]) < strlen($b[0])) ? 1 : -1;
  15. }
  16. function keylink($str,$link,$count=1)
  17. {
  18. $linkDefs = $link;
  19. $linkMap = array();
  20. foreach($linkDefs as $row) {
  21. $linkMap[] = explode(',', $row);
  22. }

  23. foreach ($linkMap as $row) {

  24. $str = preg_replace('/(s*)('.$row[0].')(s*)/sui ', '${2}', $str);
  25. }

  26. usort($linkMap, '_sortDesc');

  27. $tmpKwds = array( );

  28. foreach($linkMap as $i=>$row) {

  29. list($kwd, $url) = $row;
  30. for($j=$i+1; $j$subKwd = $linkMap[$j][0];
  31. //If it contains other keywords, temporarily replace it with other strings
  32. if(strpos($kwd, $subKwd) !== false) {
  33. $tmpKwd = '{'.md5($subKwd).'}';
  34. $kwd = str_replace($subKwd, $tmpKwd, $kwd);
  35. $tmpKwds[$tmpKwd] = $subKwd;
  36. }
  37. }
  38. //Replace text with links
  39. $str = preg_replace('/('.$row[0].')/sui', ''.$kwd.'', $str, $count);
  40. }

  41. //Replace sub Keyword string replacement

  42. foreach($tmpKwds as $tmp=>$kwd) {
  43. $str = str_replace($tmp, $kwd, $str);
  44. }
  45. return $str;
  46. }
  47. ?> ;

Copy code


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn