PHP automatically to add keywords to the article link function code _php tips

Source: Internet
Author: User
Copy Code code as follows:

<?php
$link = Array (
' Baidu, http://www.baidu.com/',
' Server software, http://s.jb51.net/',
' Cloud-dwelling community, http://www.jb51.net/',
);
$STR = ' Search Server Software in Baidu can go to cloud habitat community to provide software <br/>
Summer is no longer wearing stockings in recent years has become a fashion, even in the autumn is no exception. <br/>
Experts put forward, in fact, this fashion is not conducive to the United States legs. It is reported that sedentary long station lack of movement and other causes easily lead to varicose veins, especially long-term standing work of the people, easy to cause the veins of the lower extremity expansion, extension or venous valve damage to the onset. The concrete manifestation is the leg is swollen, the vein of the lower limb resembles earthworm shape or knot is formed, the skin is purple, especially ankle and calf inside more serious. <br/>
Experts pointed out that the long-term standing work of people more or less have some lower extremity varicose veins, but the degree of severity is not the same. Varicose veins of the lower extremity is a more troublesome treatment of disease, serious needs of surgery, so the best method or preventive measures, in the absence of obvious symptoms, take precautions, wear long tube stockings is a good way to help the blood into the larger and deeper veins. <br/>
Maintain the ideal weight, can reduce the chance of varicose veins, avoid tight clothing, wear medical stockings, so as not to accumulate blood in the legs; beware of contraceptive pills, some of which may cause this problem. ';
$out =keylink ($str, $link, 1); $str the original character $link, replace the linked array, 3 replacement times
Echo $out;
function _sortdesc ($a, $b) {
Return (strlen ($a [0]) < strlen ($b [0]))? 1:-1;
}
function Keylink ($str, $link, $count =1)
{
$linkDefs = $link;
$linkMap = Array ();
foreach ($linkDefs as $row) {
$linkMap [] = Explode (', ', $row);
}

foreach ($linkMap as $row) {
$str = Preg_replace ('/(<a.*?>\s*) ('. $row [0]. ') (\s*<\/a>)/sui ', ' ${2} ', $str);
}

Usort ($linkMap, ' _sortdesc ');

$tmpKwds = Array ();

foreach ($linkMap as $i => $row) {
List ($KWD, $url) = $row;
for ($j = $i +1; $j <count ($LINKMAP); $j + +) {
$SUBKWD = $linkMap [$j][0];
If you include another keyword, temporarily replace it with another string
if (Strpos ($KWD, $SUBKWD)!== false) {
$tmpKwd = ' {'. MD5 ($SUBKWD). '} ';
$KWD = Str_replace ($SUBKWD, $TMPKWD, $KWD);
$tmpKwds [$TMPKWD] = $SUBKWD;
}
}
Replace the text with a link
$str = Preg_replace ('/('. $row [0]. ') /sui ', ' <a href= '. $row [1]. ' target= ' _blank ' > '. $kwd. ' </a> ', $str, $count);
}

Replace the string for the sub-keyword back
foreach ($tmpKwds as $tmp => $kwd) {
$str = Str_replace ($tmp, $KWD, $STR);
}
return $str;
}
?>

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.