PHP Regular Expression replaces the site keyword link with a blank solution, regular expression keyword

Source: Internet
Author: User
Tags php regular expression

PHP Regular Expression replaces the site keyword link with a blank solution, regular expression keyword

The title is inappropriate. The specific situation is as follows: the website needs to add the keyword link function, then it needs to match and replace the content of the article with a regular expression, and then uses the preg_replace function. The replaced program code is as follows:

Function ReplaceKeyword ($ linkDefs, $ content) {$ linkMap = array ();/* foreach ($ linkDefs as $ row) {$ linkMap [] = explode (',', $ row);} */$ linkMap = $ linkDefs; // replace the original link with the text foreach ($ linkMap as $ row) {$ content = preg_replace ('/(<. *?> \ S *)('. $ row [0]. ') (\ s * <\/a>)/sui', $ row [0], $ content);} // sort usort ($ linkMap, '_ sortDesc'); // var_dump ($ linkMap); $ tmpKwds = array (); // store the temporarily replaced subkeyword $ k_count = 0; foreach ($ linkMap as $ I =>$ row) {list ($ kwd, $ url) = $ row; for ($ j = $ I + 1; $ j <count ($ linkMap); $ j ++) {$ subKwd = $ linkMap [$ j] [0]; // if other keywords are included, temporarily replace it with other strings, such as tea leaves into if (strpos ($ kwd, $ subKwd )! = False) {$ tmpKwd = ''; $ kwd = str_replace ($ subKwd, $ tmpKwd, $ kwd); $ tmpKwds [$ tmpKwd] = $ subKwd ;}} // Replace the text with the link require (MLEINC. '/config/globals. config. php '); $ th_num = $ config ['keyword _ num']; // number of times the keyword is replaced $ content = preg_replace ('/('. $ row [0]. ')/sui',' <a href = "'. $ row [1]. '"> '. $ kwd. '</a>', $ content, $ th_num, $ count); // all matching items will be replaced by $ k_count + = $ count ;} // Replace the string that replaces the subkeyword with foreach ($ tmpKwds as $ tmp => $ kwd) {$ content = str_replace ($ tmp, $ kwd, $ content );} $ result = array ($ content, $ k_count); return $ result; unset ($ result); unset ($ tmp); unset ($ tmpKwds); unset ($ kwd ); unset ($ count); unset ($ config); unset ($ linkMap); unset ($ linkDefs); unset ($ tmpKwd); unset ($ content ); unset ($ th_num); unset ($ row); unset ($ k_count );}

The program is found on the Internet, and then tested locally. The local environment serves php 5.3 as 5.2. After uploading the program to the Internet, it is blank when it is submitted, the first consideration was the PHP version. I thought it was the difference between ereg preg and it would not work after replacement. Later, I checked it online and found that some netizens said they could adjust the large pcre. backtrack_limit and pcre. recursion_limit. I tried it and it was okay. It seems to be a configuration problem, but in general, the default configuration of PHP should be no problem, I wrote this program is still not good enough!


Php Regular Expression problem. Below is a program that replaces the keyword with the link

I can write it like this. I tried IE6 and FIREFOX 3.0. No problem.
<! Doctype html public "-// W3C // dtd html 4.0 Transitional // EN">
<HTML>
<HEAD>
<TITLE> New Document </TITLE>
<Meta name = "Generator" CONTENT = "EditPlus">
<Meta name = "Author" CONTENT = "">
<Meta name = "Keywords" CONTENT = "">
<Meta name = "Description" CONTENT = "">
</HEAD>

<BODY>
Xhtml Linux
</BODY>
<Script language = "JavaScript">
Document. body. innerHTML = document. body. innerHTML. replace (/(x) | (l)/gi, "<font color = red> $1 $2 </font> ");
</Script>
</HTML>

The following is the innerHTM of the BODY obtained through FIREFOX 3.0.
<Font color = "red"> X </font> htm <font color = "red"> l </font> <font color = "red"> L </font> inu <font color = "red"> x </font>
<Script> <font color = red> l </font> anguage = "JavaScript">
Document. body. innerHTM <font color = red> L </font> = document. body. innerHTM <font color = red> L </font>. rep <font color = red> l </font> ace (/(<font color = red> x </font>) | (<font color = red> l </font>)/gi, "<font co <font color = red> l </font> or = red> $1 $2 </...... remaining full text>

PHP Regular Expression replacement hyperlink

Use the str_replace Function
 

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.