Heim  >  Artikel  >  Backend-Entwicklung  >  php关键词自动添加span标签与关键词高亮代码

php关键词自动添加span标签与关键词高亮代码

WBOY
WBOYOriginal
2016-07-25 08:51:20938Durchsuche
  1. // Example use: $spanned = codeWords($string_containing_keywords);
  2. // My site: andrew.dx.am
  3. // Using colour==blue, but different arrays of words and different
  4. // colours can be added.
  5. function onlyWholeWords(&$value, $key) {
  6. // Ignores words after // comment delimiters.
  7. //$value = "/\b(" . $value . ")\b/"; // doesn't handle comments
  8. //$value = "/^(?:(?!\/\/).)*\K\b(" . $value . ")\b/";
  9. // \K lookbehind alternative is not supported in PHP $value = "/^((?:(?!\/\/).)*)\b" . $value . "\b/";
  10. }
  11. function addSpan(&$value, $key, $color='blue') {
  12. $value = "$1" . $value . "";
  13. }
  14. function codeWords($code) {
  15. $keywords = array('as', 'break', 'case', 'class',
  16. 'continue', 'default', 'do', 'elif', 'else',
  17. 'elseif', 'for', 'foreach', 'function', 'if',
  18. 'new', 'null', 'return', 'self', 'switch',
  19. 'this', 'to', 'typeof', 'until',
  20. 'var', 'void', 'while', 'with');
  21. $keywords2 = $keywords;
  22. array_walk($keywords, 'onlyWholeWords');
  23. array_walk($keywords2, 'addSpan', 'blue');
  24. $code = preg_replace($keywords, $keywords2, $code);
  25. return $code;
  26. }
复制代码

二、php自动给文章加关键词链接

自动给文章加关键词链接的php函数代码。

代码:

  1. $link = array(

  2. '百度,http://www.baidu.com/',
  3. 'php教程,http://bbs.it-home.org/wb/php/',
  4. '脚本学堂,http://bbs.it-home.org/',
  5. );
  6. $str = '在百度中搜索php教程就可以到程序员之家提供的php编程文章
  7. 此处放置需要替换的内容。';
  8. $out=keylink($str,$link,1); //$str 原始字符 $link,替换链接数组, 3替换次数
  9. echo $out;
  10. function _sortDesc($a, $b) {
  11. return (strlen($a[0]) }
  12. function keylink($str,$link,$count=1)
  13. {
  14. $linkDefs = $link;
  15. $linkMap = array();
  16. foreach($linkDefs as $row) {
  17. $linkMap[] = explode(',', $row);
  18. }
  19. foreach($linkMap as $row) {

  20. $str = preg_replace('/(\s*)('.$row[0].')(\s*)/sui', '${2}', $str);
  21. }
  22. usort($linkMap, '_sortDesc');

  23. $tmpKwds = array();

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

  25. list($kwd, $url) = $row;
  26. for($j=$i+1; $j$subKwd = $linkMap[$j][0];
  27. //如果包含其他关键字,暂时替换成其他字符串
  28. if(strpos($kwd, $subKwd) !== false) {
  29. $tmpKwd = '{'.md5($subKwd).'}';
  30. $kwd = str_replace($subKwd, $tmpKwd, $kwd);
  31. $tmpKwds[$tmpKwd] = $subKwd;
  32. }
  33. }
  34. //把文字替换成链接
  35. $str = preg_replace('/('.$row[0].')/sui', ''.$kwd.'', $str, $count);
  36. }
  37. //把代替子关键字的字符串替换回来

  38. foreach($tmpKwds as $tmp=>$kwd) {
  39. $str = str_replace($tmp, $kwd, $str);
  40. }
  41. return $str;
  42. }
  43. ?>
复制代码


Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn