-
-
/** - * PHP automatically generates internal links for keywords
- * by bbs.it-home.org
- */
- include_once(dirname(__file__)."/../db/DBViewSpot.php" );
-
class InnerLink{
- private static $spotUrlMap;
- /**
- * Generate view spots keywords link
- *
- * @param string $description
- * @param array $spotUrlMap
- * @return string
- */
- public static function genSpotLink($basePath, $description)
- {
- if(empty(InnerLink::$spotUrlMap)){
- InnerLink::$spotUrlMap = DBViewSpot::getSpotPare();
- }
- // Exclude irregular data
- if ( empty($description)) {
- return $description;
- }
- foreach (InnerLink::$spotUrlMap AS $spotUrlPair ){
- $replace = "".$spotUrlPair[0]."";
- // There are only text in the description, no pictures, so just pay attention to the a link
- $tmp1 = explode(" $is_replaced=false;
- foreach ($tmp1 as $key=>$item){
- $tmp2 = explode("
",$item);
- if (sizeof($tmp2)> 1) {
- if (substr($tmp2[0],0,1)!="a" && substr($tmp2[0],0,1)!="A"){
- if ($is_replaced== =false) {
- $tmp2[1] = InnerLink::str_replace_once($spotUrlPair[0],$replace,$tmp2[1],$is_replaced);
- }
- $tmp1[$key] = implode("< /a>",$tmp2);
- }
- }else {
- if (is_string($item) && $is_replaced===false) {
- $tmp1[$key] = InnerLink::str_replace_once($spotUrlPair[0] ,$replace,$item,$is_replaced);
- }
- }
- }
- $description = implode(" }
- return $description;
- }
- /**
- * replace key word for one time
- *
- * @param string $needle
- * @param string $replace
- * @param string $haystack
- * @param bool $is_replaced
- * @return string
- * /
- private static function str_replace_once($needle, $replace, $haystack,&$is_replaced) {
- $pos = strpos($haystack, $needle);
- if ($pos === false) {
- return $haystack;
- }
- $is_replaced=true;
- return substr_replace($haystack, $replace, $pos, strlen($needle));
- }
- }
-
Copy code
|