首頁  >  文章  >  後端開發  >  PHP自動產生關鍵字內鏈的類

PHP自動產生關鍵字內鏈的類

WBOY
WBOY原創
2016-07-25 08:57:371387瀏覽
  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("",$item);
  30. if (sizeof($tmp2)>1) {
  31. if (substr($tmp2[0],0,1)!="a" && substr($tmp2[0],0,1)!="A"){
  32. if ($is_replaced===false) {
  33. $tmp2[1] = InnerLink::str_replace_once($spotUrlPair[0],$replace,$tmp2[1],$is_replaced);
  34. }
  35. $tmp1[$key] = implode("",$tmp2);
  36. }
  37. }else {
  38. if (is_string($item) && $is_replaced===false) {
  39. $tmp1[$key] = InnerLink::str_replace_once($spotUrlPair[0],$replace,$item,$is_replaced);
  40. }
  41. }
  42. }
  43. $description = implode(" }
  44. return $description;
  45. }
  46. /**
  47. * replace key word for one time
  48. *
  49. * @param string $needle
  50. * @param string $replace
  51. * @param string $haystack
  52. * @param bool $is_replaced
  53. * @return string
  54. */
  55. private static function str_replace_once($needle, $replace, $haystack,&$is_replaced) {
  56. $pos = strpos($haystack, $needle);
  57. if ($pos === false) {
  58. return $haystack;
  59. }
  60. $is_replaced=true;
  61. return substr_replace($haystack, $replace, $pos, strlen($needle));
  62. }
  63. }

复制代码


陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn