Home > Backend Development > PHP Tutorial > PHP implements adding internal links to article content keywords_PHP tutorial

PHP implements adding internal links to article content keywords_PHP tutorial

WBOY
Release: 2016-07-13 10:43:41
Original
1792 people have browsed it

Website articles automatically add links within the site. I think most of my classmates’ websites have this function. The editor below summarizes three functions for automatically adding links within the article. From top to bottom, the best method is at the top.


Example 1

The code is as follows Copy code
 代码如下 复制代码

/**
*对内容中的关键词添加链接
*只处理第一次出现的关键词,对已有链接的关键不会再加链接,支持中英文
*$content:string 原字符串
*$keyword:string  关键词
*$link:string,链接
*/
function yang_keyword_link($content,$keyword,$link){
    //排除图片中的关键词
    $content = preg_replace( '|(]*?)('.$keyword.')([^>]*?>)|U', '%&&&&&%', $content);
    $regEx = '/(?!((<.*?)|(]*?)>)|([^>]*?))/si';
    $url=''.$keyword.'';
    $content = preg_replace($regEx,$url,$content,1);
    //还原图片中的关键词
    $content=str_replace('%&&&&&%',$keyword,$content);
    return $content;
}

/** *Add links to keywords in the content *Only process the keywords that appear for the first time. Keywords that already have links will not be linked again. Chinese and English are supported *$content:string original string *$keyword:string Keyword *$link:string, link */ function yang_keyword_link($content,$keyword,$link){ //Exclude keywords in images $content = preg_replace( '|(]*?)('.$keyword.')([^>]*?>)|U', '$1%&&&&&%$3', $content); $regEx = '/(?!((<.*?)|(]*?) >)|([^>]*?))/si'; $url=''.$keyword.''; $content = preg_replace($regEx,$url,$content,1); //Restore the keywords in the picture $content=str_replace('%&&&&&%',$keyword,$content); Return $content; }


Example 2

The code is as follows Copy code


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]."";
// The description is only text, there is no picture, 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("",$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));
    }
}

Example 3

This was written when I first learned PHP, and I felt there was something wrong with it

代码如下 复制代码
$keys =array(
array('网页特效','/js_a/js.html'),
array('seo','/seo/seo.html'),
array('php','/phper/php.html'),
array('jsp','/jsp/jsp.html'),
array('asp','/asp/asp.html'),
array('ps','/fw/photo.html'),
array('photoshop','/fw/photo.html'),
array('javascript','/js_a/js.html'),
array('.net','/net/net.html'),
array('非主流','/fw/photo.html'),
array('网络','/mon/mon.html'),
array('css','/cssdiv/css.html'),
array('平面设计','/fw/photo.html'),
array('网站','/person/'),
array('网页制作','/wy/yw.html'),
array('搜索引擎','/seo/seo.html'),
array('优化','/seo/seo.html'),
array('动画','/flash_a/flash.html'),
array('数据库','/database/database.html'),
array('挣钱','/mon/mon.html'),
array('运营','/mon/mon.html')

);

$str ="今天是2010年5月30号,我的网站出现的问题这对seo有很多的问题,seo就是搜索引擎优化了,以前学php好啊现在觉得jsp好,css+div,网页,网页设计,网页制作,网页学习,网页教学,Photoshop,Flash,HTML,CSS,Dreamweaver,Fireworks,ASP,PHP,JSP,ASP.NET,网站建设,网站开发,网页特效,平面设计,个人网站,网页素材";


echo $str,"
"; 
    foreach($keys as $nkeys){ 
           //print_r($nkeys);echo"
"; 
           //foreach( $nkeys as $join) { 
           //echo($join),"
"; 
           if(strpos($str,$nkeys[0]) ){ 
                 $str =str_replace($nkeys[0],"".$nkeys[0]."",$str);      
           } 
           //} 
    } 
         
       echo $str; 
    ?>

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/633156.htmlTechArticleWebsite articles automatically add in-site links. I think most of the websites of my classmates have this function. The editor summarizes it below There are three functions of automatically adding links within articles, from top to bottom, the best...
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template