Common PHP link code. Method 1: manually edit and add Method 2: use the program to output content without affecting the editing of background content. The following describes how PHP automatically adds an Association link to the article content and the example 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;
}
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...