使用数组preg_replace到preg_replace_calback

时间:2015-01-07 07:44:46

标签: php

我要将preg_replace代码更改为preg_replace_callback. preg_replace代码

function replaceChars($text) {
// $text should contain an HTML document/content.
// This will remove HTML tags, javascript sections
// and white space. It will also convert some
// common HTML entities to their text equivalent.

$search = array ("'&(quot|#34);'i",  
                 "'&(amp|#38);'i",
                 "'&(lt|#60);'i",
                 "'&(gt|#62);'i",
                 "'&(nbsp|#160);'i",
                 "'&(iexcl|#161);'i",
                 "'&(cent|#162);'i",
                 "'&(pound|#163);'i",
                 "'&(copy|#169);'i",
                 "'&#(\d+);'e"); 

$replace = array ("\"",
                  "&",
                  "<",
                  ">",
                  " ",
                  chr(161),
                  chr(162),
                  chr(163),
                  chr(169),
                  "chr(\\1)");

$text = preg_replace ($search, $replace, $text);
$text = html_entity_decode ( $text , ENT_QUOTES , 'UTF-8' );
return $text;
}

preg_replace_callback代码

return preg_replace_callback (  ($search) , function ($matches) use ($replace) {
        return ( ( isset ( $replace[$matches[1]] ) ) ? $replace[$matches[1]] : '' );
    } , $text );

我收到警告修改器/ e不能与行号中的替换回调一起使用。

0 个答案:

没有答案