当前位置:Gxlcms > PHP教程 > php添加关联链接的代码

php添加关联链接的代码

时间:2021-07-01 10:21:17 帮助过:3人阅读

  1. /**
  2. * php关联链接
  3. */
  4. header("Content-Type:text/html;charset=utf-8"); //设置编码
  5. $linkdatas=array(
  6. array('前端开发', 'http://http://bbs.it-home.org'),
  7. array('前端设计', 'http://bbs.it-home.org'),
  8. array('web前端', 'http://bbs.it-home.org'),
  9. array('前端博客', 'http://bbs.it-home.org'),
  10. );
  11. echo "替换前
    ";
  12. echo $str='Web前端开发 - 专注于网站前端设计与Web用户体验。前端开发,专注Web前端开发,关注Web用户体验,关注国内外最新最好的前端设计资源和前端开发技术的专业前端博客';
  13. echo "
    替换后
    ";
  14. echo $str=keylinks($str,2);
  15. /**
  16. * 关联关键词替换
  17. * @param txt $string 原字符串
  18. * @param replacenum $int 替换次数
  19. * @return string 返回字符串
  20. */
  21. function keylinks($txt, $replacenum = '') {
  22. global $linkdatas;
  23. if ($linkdatas) {
  24. $word = $replacement = array();
  25. foreach ($linkdatas as $v) {
  26. $word1[] = '/(?!()/s';
  27. $word2[] = $v[0];
  28. $replacement[] = '' . $v[0] . '';
  29. }
  30. if ($replacenum != '') {
  31. $txt = preg_replace($word1, $replacement, $txt, $replacenum);
  32. } else {
  33. $txt = str_replace($word2, $replacement, $txt);
  34. }
  35. }
  36. return $txt;
  37. }
  38. ?>

人气教程排行