Home  >  Article  >  Backend Development  >  PHP automatically generates classes for keyword internal links

PHP automatically generates classes for keyword internal links

WBOY
WBOYOriginal
2016-07-25 08:57:371387browse
  1. /**

  2. * PHP automatically generates internal links for keywords
  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. // Exclude irregular data
  21. if ( empty($description)) {
  22. return $description;
  23. }
  24. foreach (InnerLink::$spotUrlMap AS $spotUrlPair ){
  25. $replace = "".$spotUrlPair[0]."";
  26. // There are only text in the description, no pictures, so just pay attention to the a link
  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("< /a>",$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. }

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