Home >Backend Development >PHP Tutorial >PHP regular expression replaces the problem of blank space after site keyword link, regular expression keyword_PHP tutorial

PHP regular expression replaces the problem of blank space after site keyword link, regular expression keyword_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:19:15763browse

The problem of blank space after PHP regular expression replacement site keyword link is solved. The regular expression keyword

title is not suitable or not. The specific situation is this: the website wants to add a keyword link function, and then the content of the article needs to be matched and replaced with regular expressions, and then the preg_replace function is used. 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 text foreach($linkMap as $row) { $content = preg_replace('/(s*)('.$row[0].')(s*)/sui', $row[0], $ content); } ​ ​ ​ //Sort keywords from longest to shortest usort($linkMap, '_sortDesc'); //var_dump($linkMap); ​ $tmpKwds = array(); //Storage temporarily replaced sub-keywords ​ $k_count=0; foreach($linkMap as $i=>$row) { list($kwd, $url) = $row; for($j=$i+1; $j'.$kwd.'', $content, $th_num ,$count); // All matching items will be replaced $k_count+=$count; } ​ //Replace the string that replaces the subkeyword back 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 was found online and tested locally. It was normal. The local environment was PHP 5.3 and the service was 5.2. After uploading it to the Internet, the submission showed blank. At first, I thought it was a PHP version problem and thought it was the difference between ereg and preg. , it still doesn’t work after replacement. Later, when I looked online, I found that some netizens said that just adjusting the pcre.backtrack_limit and pcre.recursion_limit would be enough. I tried it, and it worked. It seems to be a configuration problem, but under normal circumstances, the default configuration of PHP should be fine. The program I wrote myself is still not good enough!

php regular expression problem, here is a program that replaces keywords with links

I can write like this. I have tried it with IE6 and FIREFOX 3.0, and there is no problem.



New Document < ;/TITLE><br> <META NAME="Generator" CONTENT="EditPlus"><br> <META NAME="Author" CONTENT=""><br> <META NAME="Keywords" CONTENT=""><br> <META NAME="Description" CONTENT=""><br> </HEAD><br><br> <BODY><br> Xhtml Linux <br> < ;/BODY><br> <script language="JavaScript"> <br> document.body.innerHTML = document.body.innerHTML.replace(/(x)|(l)/gi,"<font color =red>$1$2</font>"); <br></script><br></HTML><br><br>The following is the innerHTM of BODY obtained through FIREFOX 3.0<br> < font color="red"> ">x</font> <br> <script><font color=red>l</font>anguage="JavaScript"> <br> 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</...the rest Full text>><br/> </br> <div class="header2"><span class="icon i-relatedanswer"><h3>Replacement of regular expressions in PHP A HREF problem</h3></span></div><div class="best-replyer"></div> <p class="ft p1">What do you mean, what do you want to replace?<br />Remove this style="text-decoration: none"? <br /><br />$str = "<a href='xxxxx' target='_blank' style='text-decoration: none;'>xxxx</a>";<br>echo preg_replace("/style=.+?['|\"]/",'title="xxxx"',$ str);<br>What about this? <br> </p> <p align="left"></p> <div style="display:none;">http://www.bkjia.com/PHPjc/876329.html<span id="url" itemprop="url"></span>www.bkjia.com<span id="indexUrl" itemprop="indexUrl"></span>true<span id="isOriginal" itemprop="isOriginal"></span>http: //www.bkjia.com/PHPjc/876329.html<span id="isBasedOnUrl" itemprop="isBasedOnUrl"></span>TechArticle<span id="genre" itemprop="genre"></span>Solution to the problem of blank spaces after replacing site keyword links with PHP regular expressions. I don’t know about the regular expression keyword title. Suitable or not. The specific situation is this: the website needs to add custom...<span id="description" itemprop="description"></span> </div> <div class="art_confoot"></div></div><div class="nphpQianMsg"><div class="clear"></div></div><div class="nphpQianSheng"><span>Statement:</span><div>The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn</div></div></div><div class="nphpSytBox"><span>Previous article:<a class="dBlack" title="PHP array sorting sort, asort and ksort usage, asortksort_PHP tutorial" href="https://m.php.cn/faq/293812.html">PHP array sorting sort, asort and ksort usage, asortksort_PHP tutorial</a></span><span>Next article:<a class="dBlack" title="PHP array sorting sort, asort and ksort usage, asortksort_PHP tutorial" href="https://m.php.cn/faq/293814.html">PHP array sorting sort, asort and ksort usage, asortksort_PHP tutorial</a></span></div><div class="nphpSytBox2"><div class="nphpZbktTitle"><h2>Related articles</h2><em><a href="https://m.php.cn/article.html" class="bBlack"><i>See more</i><b></b></a></em><div class="clear"></div></div><ins class="adsbygoogle" style="display:block" data-ad-format="fluid" data-ad-layout-key="-6t+ed+2i-1n-4w" data-ad-client="ca-pub-5902227090019525" data-ad-slot="8966999616"></ins><script> (adsbygoogle = window.adsbygoogle || []).push({}); </script><ul class="nphpXgwzList"><li><b></b><a href="https://m.php.cn/faq/1.html" title="How to use cURL to implement Get and Post requests in PHP" class="aBlack">How to use cURL to implement Get and Post requests in PHP</a><div class="clear"></div></li><li><b></b><a href="https://m.php.cn/faq/1.html" title="How to use cURL to implement Get and Post requests in PHP" class="aBlack">How to use cURL to implement Get and Post requests in PHP</a><div class="clear"></div></li><li><b></b><a href="https://m.php.cn/faq/1.html" title="How to use cURL to implement Get and Post requests in PHP" class="aBlack">How to use cURL to implement Get and Post requests in PHP</a><div class="clear"></div></li><li><b></b><a href="https://m.php.cn/faq/1.html" title="How to use cURL to implement Get and Post requests in PHP" class="aBlack">How to use cURL to implement Get and Post requests in PHP</a><div class="clear"></div></li><li><b></b><a href="https://m.php.cn/faq/2.html" title="All expression symbols in regular expressions (summary)" class="aBlack">All expression symbols in regular expressions (summary)</a><div class="clear"></div></li></ul></div></div><ins class="adsbygoogle" style="display:block" data-ad-format="autorelaxed" data-ad-client="ca-pub-5902227090019525" data-ad-slot="5027754603"></ins><script> (adsbygoogle = window.adsbygoogle || []).push({}); </script><footer><div class="footer"><div class="footertop"><img src="/static/imghwm/logo.png" alt=""><p>Public welfare online PHP training,Help PHP learners grow quickly!</p></div><div class="footermid"><a href="https://m.php.cn/about/us.html">About us</a><a href="https://m.php.cn/about/disclaimer.html">Disclaimer</a><a href="https://m.php.cn/update/article_0_1.html">Sitemap</a></div><div class="footerbottom"><p> © php.cn All rights reserved </p></div></div></footer><script>isLogin = 0;</script><script type="text/javascript" src="/static/layui/layui.js"></script><script type="text/javascript" src="/static/js/global.js?4.9.47"></script></div><script src="https://vdse.bdstatic.com//search-video.v1.min.js"></script><link rel='stylesheet' id='_main-css' href='/static/css/viewer.min.css' type='text/css' media='all'/><script type='text/javascript' src='/static/js/viewer.min.js?1'></script><script type='text/javascript' src='/static/js/jquery-viewer.min.js'></script><script>jQuery.fn.wait = function (func, times, interval) { var _times = times || -1, //100次 _interval = interval || 20, //20毫秒每次 _self = this, _selector = this.selector, //选择器 _iIntervalID; //定时器id if( this.length ){ //如果已经获取到了,就直接执行函数 func && func.call(this); } else { _iIntervalID = setInterval(function() { if(!_times) { //是0就退出 clearInterval(_iIntervalID); } _times <= 0 || _times--; //如果是正数就 -- _self = $(_selector); //再次选择 if( _self.length ) { //判断是否取到 func && func.call(_self); clearInterval(_iIntervalID); } }, _interval); } return this; } $("table.syntaxhighlighter").wait(function() { $('table.syntaxhighlighter').append("<p class='cnblogs_code_footer'><span class='cnblogs_code_footer_icon'></span></p>"); }); $(document).on("click", ".cnblogs_code_footer",function(){ $(this).parents('table.syntaxhighlighter').css('display','inline-table');$(this).hide(); }); $('.nphpQianCont').viewer({navbar:true,title:false,toolbar:false,movable:false,viewed:function(){$('img').click(function(){$('.viewer-close').trigger('click');});}}); </script></body><!-- Matomo --><script> var _paq = window._paq = window._paq || []; /* tracker methods like "setCustomDimension" should be called before "trackPageView" */ _paq.push(['trackPageView']); _paq.push(['enableLinkTracking']); (function() { var u="https://tongji.php.cn/"; _paq.push(['setTrackerUrl', u+'matomo.php']); _paq.push(['setSiteId', '9']); var d=document, g=d.createElement('script'), s=d.getElementsByTagName('script')[0]; g.async=true; g.src=u+'matomo.js'; s.parentNode.insertBefore(g,s); })(); </script><!-- End Matomo Code --></html>