PHP implementation of the article content keyword added inner chain _php tutorial

Source: Internet
Author: User
Website articles automatically add links to the site this I think most of the students have this function, the following small series summed up the three kinds of articles within the chain automatically added function, from the top down, the best method on the top.


Example 1

The code is as follows Copy Code

/**
* Add links to keywords in content
* Only deal with the first occurrence of keywords, the key to the existing link will not be linked to support Chinese and English
* $content: string literal
* $keyword: string keyword
* $link: String, link
*/
function Yang_keyword_link ($content, $keyword, $link) {
Exclude keywords from a picture
$content = Preg_replace (' | () *?) ('. $keyword. ') ([^>]*?>) | U ', ' $1%&&&&&%$3 ', $content);
$regEx = '/(?! (<.*?) | (]*?) >) | ([^>]*?)] /si ';
$url = ". $keyword.";
$content = Preg_replace ($regEx, $url, $content, 1);
Restore keywords in a 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]. "";
Description inside only text, no picture, so just note a link
$tmp 1 = Explode ("$is _replaced=false;
foreach ($tmp 1 as $key = = $item) {
$tmp 2 = Explode ("", $item);
if (sizeof ($tmp 2) >1) {
if (substr ($tmp 2[0],0,1)! = "a" && substr ($tmp 2[0],0,1)! = "a") {
if ($is _replaced===false) {
$tmp 2[1] = innerlink::str_replace_once ($spotUrlPair [0], $replace, $tmp 2[1], $is _replaced);
}
$tmp 1[$key] = Implode ("", $tmp 2);
}
}else {
if (is_string ($item) && $is _replaced===false) {
$tmp 1[$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 is what I wrote when I first learned PHP, and I feel a bit of a problem.

The code is as follows Copy Code
$keys =array (
Array (' Web effects ', '/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 (' Non-mainstream ', '/fw/photo.html '),
Array (' Network ', '/mon/mon.html '),
Array (' CSS ', '/cssdiv/css.html '),
Array (' Graphic design ', '/fw/photo.html '),
Array (' website ', '/person/'),
Array (' Web authoring ', '/wy/yw.html '),
Array (' Search engine ', '/seo/seo.html '),
Array (' Optimization ', '/seo/seo.html '),
Array (' animation ', '/flash_a/flash.html '),
Array (' database ', '/database/database.html '),
Array (' Earn money ', '/mon/mon.html '),
Array (' Operations ', '/mon/mon.html ')

);

$STR = "Today is May 30, 2010, My site problems this SEO has a lot of problems, SEO is SEO, before learning php good AH now think JSP good, Css+div, Web page, Web design, web production, Web learning, Web teaching, Photoshop,flash,html,css,dreamweaver,fireworks,asp,php,jsp,asp.net, website construction, website development, web effects, graphic design, personal website, web material ";


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;
?>

http://www.bkjia.com/PHPjc/633156.html www.bkjia.com true http://www.bkjia.com/PHPjc/633156.html techarticle website articles automatically add links to the site this I think most of the students have this function, the following small series summed up the three kinds of articles within the chain automatically added function, from the top down, the best ...

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.