PHP Association Link Add Method

Source: Internet
Author: User

In order to optimize the chain, we need to add content to the key link, that content if you add links, how to add it?

Method One: Manually edit add

Method Two: The use of the program in the output content implementation, without affecting the background content of the edit.

Here's how PHP automatically adds links to article content and examples:

Keylinks Function Method:

Parameter one: the content to be processed

Parameter two: Replacement times

Return Result: Processed content

Header ("Content-type:text/html;charset=utf-8"); Set encoding

$linkdatas =array (

Array (' Front-End development ', ' http://blog.kgula.com '),

Array (' Front design ', ' http://blog.kgula.com '),

Array (' Web Front end ', ' http://blog.kgula.com '),

Array (' Front-End blog ', ' http://blog.kgula.com '),

);

echo "replace before <br/>";

echo $str = ' Web front-end development-focus on site front-end design and Web user experience. Front-End development, focus on the Web front-end development, focus on the Web user experience, focus on the latest and best front-end design resources and front-end development Technology professional front-end blog ';

echo "<br/> replace <br/>";

echo $str =keylinks ($STR, 2);

/**

* Associated keyword Replacement

* @param txt $string original string

* @param replacenum $int replacement times

* @return String returns strings

*/

function Keylinks ($txt, $replacenum = ' ") {

Global $linkdatas;

if ($linkdatas) {

$word = $replacement = Array ();

foreach ($linkdatas as $v) {

$word 1[] = '/(?! (<a.*?)) ' . Preg_quote ($v [0], '/'). '(?!. *</a>)/s ';

$word 2[] = $v [0];

$replacement [] = ' <a href= '. $v [1]. ' "target=" _blank "class=" Keylink ">". $v [0]. ' </a> ';

}

if ($replacenum!= ') {

$txt = Preg_replace ($word 1, $replacement, $txt, $replacenum);

} else {

$txt = Str_replace ($word 2, $replacement, $txt);

}

}

return $txt;

}

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.