search
Homephp教程php手册PHP正则表达式替换站点关键字链接后空白的问题解决,正则表达式关键字

PHP正则表达式替换站点关键字链接后空白的问题解决,正则表达式关键字

标题这样不知道合适不合适。具体的情况是这样的:网站要增加关键字链接功能,然后需要对文章的内容进行正则表达式匹配并替换,然后使用了preg_replace函数。替换的程序代码如下:

function ReplaceKeyword($linkDefs,$content){ $linkMap = array();   /*foreach($linkDefs as $row) {  $linkMap[] = explode(',', $row); }*/ $linkMap = $linkDefs;   //把原有的链接替换成文字 foreach($linkMap as $row) {  $content = preg_replace('/(\s*)('.$row[0].')(\s*)/sui', $row[0], $content); }       //关键字从长至短排序 usort($linkMap, '_sortDesc'); //var_dump($linkMap);   $tmpKwds = array(); //存放暂时被替换的子关键字   $k_count=0; foreach($linkMap as $i=>$row) {  list($kwd, $url) = $row;  for($j=$i+1; $j'.$kwd.'', $content, $th_num ,$count);  // 所有的匹配项都会被替换  $k_count+=$count; }   //把代替子关键字的字符串替换回来 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);

 

}   程序是从网上找的,然后在本地测试是正常的,本地环境为php 5.3 服务是5.2的,上传到网上去后,提交则显示空白,一开始考虑是PHP版本问题,以为是ereg preg的区别,替换后还是不行。后来网上看,发现有网友说调整大pcre.backtrack_limit和pcre.recursion_limit就行,我试了下,果然可以了。看来是配置问题,不过一般情况下,PHP的默认配置应该没啥问题,我自己写的这程序还是不够好!

php正则表达式问题,下面是一个,替换关键字为链接的程序

我这样写都可以啊,用IE6,FIREFOX 3.0 都试过了,没问题。
#47;/W3C//DTD HTML 4.0 Transitional//EN">


New Document <br> <meta name="Generator" content="EditPlus"> <br> <meta name="Author" content=""> <br> <meta name="Keywords" content=""> <br> <meta name="Description" content=""> <br> <br><br>
Xhtml Linux

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)