Heim  >  Artikel  >  Backend-Entwicklung  >  PHP自动生成关键字内链的类

PHP自动生成关键字内链的类

WBOY
WBOYOriginal
2016-07-25 08:57:371385Durchsuche
  1. /**

  2. * php自动生成关键字的内链
  3. * by bbs.it-home.org
  4. */
  5. include_once(dirname(__file__)."/../db/DBViewSpot.php" );
  6. class InnerLink{

  7. private static $spotUrlMap;
  8. /**
  9. * Generate view spots keywords link
  10. *
  11. * @param string $description
  12. * @param array $spotUrlMap
  13. * @return string
  14. */
  15. public static function genSpotLink($basePath, $description)
  16. {
  17. if(empty(InnerLink::$spotUrlMap)){
  18. InnerLink::$spotUrlMap = DBViewSpot::getSpotPare();
  19. }
  20. // 排除不规则数据
  21. if ( empty($description)) {
  22. return $description;
  23. }
  24. foreach (InnerLink::$spotUrlMap AS $spotUrlPair){
  25. $replace = "".$spotUrlPair[0]."";
  26. // 描述里面只有文字,没有图片,所以只要注意 a 链接
  27. $tmp1 = explode(" $is_replaced=false;
  28. foreach ($tmp1 as $key=>$item){
  29. $tmp2 = explode("
  30. ",$item);
  31. if (sizeof($tmp2)>1) {
  32. if (substr($tmp2[0],0,1)!="a" && substr($tmp2[0],0,1)!="A"){
  33. if ($is_replaced===false) {
  34. $tmp2[1] = InnerLink::str_replace_once($spotUrlPair[0],$replace,$tmp2[1],$is_replaced);
  35. }
  36. $tmp1[$key] = implode("",$tmp2);
  37. }
  38. }else {
  39. if (is_string($item) && $is_replaced===false) {
  40. $tmp1[$key] = InnerLink::str_replace_once($spotUrlPair[0],$replace,$item,$is_replaced);
  41. }
  42. }
  43. }
  44. $description = implode(" }
  45. return $description;
  46. }
  47. /**
  48. * replace key word for one time
  49. *
  50. * @param string $needle
  51. * @param string $replace
  52. * @param string $haystack
  53. * @param bool $is_replaced
  54. * @return string
  55. */
  56. private static function str_replace_once($needle, $replace, $haystack,&$is_replaced) {
  57. $pos = strpos($haystack, $needle);
  58. if ($pos === false) {
  59. return $haystack;
  60. }
  61. $is_replaced=true;
  62. return substr_replace($haystack, $replace, $pos, strlen($needle));
  63. }
  64. }
复制代码


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