Common PHP link code

Source: Internet
Author: User
To optimize the internal link, we need to add the key link to the content. how can we add the link if the content is added? Method 1: manually edit and add
Method 2: use a program to output content without affecting the editing of background content.

The following is an example of PHP automatically adding an Association link to the article content:

Keylinks function method:
Parameter 1: content to be processed
Parameter 2: Number of replacements
Returned results: processed content

The code is as follows:


Header ("Content-Type: text/html; charset = utf-8"); // sets the encoding
$ Linkdatas = array (
Array ('frontend developer', 'http: // http://www.jb51.net '),
Array ('front-end design', 'http: // www.jb51.net '),
Array ('web frontend ', 'http: // www.jb51.net '),
Array ('frontend blog', 'http: // www.jb51.net '),
);
Echo "before replacement
";
Echo $ str = 'web front-end development-focuses on website front-end design and Web user experience. Front-end development: focuses on Web front-end development, pays attention to Web user experience, and pays attention to the latest and best front-end design resources and professional front-end blogs of front-end development technologies at home and abroad ';
Echo"
After replacement
";
Echo $ str = keylinks ($ str, 2 );
/**
* Link keyword replacement
* @ Param txt $ string original string
* @ Param replacenum $ number of int replacements
* @ Return string returns a string.
*/
Function keylinks ($ txt, $ replacenum = ''){
Global $ linkdatas;
If ($ linkdatas ){
$ Word = $ replacement = array ();
Foreach ($ linkdatas as $ v ){
$ Word1 [] = '/(?! ()/S ';
$ Word2 [] = $ v [0];
$ Replacement [] = ''. $ v [0].'';
}
If ($ replacenum! = ''){
$ Txt = preg_replace ($ word1, $ replacement, $ txt, $ replacenum );
} Else {
$ Txt = str_replace ($ word2, $ 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.