求助数组里的字符串替换

浏览:2588 发布日期:2013/08/01 分类:求助交流 关键字: 正则 替换
我从文本中匹配出一个数组,然后对这个数组进行处理(htmlentities,nl2br),然后,再将替换后的数组分别放会原处。

现在困到将数组放回,我用现在循环,使用替换函数替换回去。

这样就遇到了问题,

使用preg_replace可以保留上面(htmlentities,nl2br)的处理,但是所有匹配值都成了数组的最后已项,

如果使用str_replace,可以将所有匹配值都放回原来的地方,但是(htmlentities,nl2br)的处理都失效了。

下面是这个思路的代码。<?php

function ubb_php($tex){
preg_match_all("/\[code\]([\s\S]*?)\[\/code\]/si",$tex,$code);
$code=$code[0];
$code = array_map("htmlentities",$code);
$code = array_map("nl2br",$code);
foreach($code as $key =>$value){
print_r($value);echo '<br>';
    $tex = preg_replace("/\[code\]([\s\S]*?)\[\/code\]/si",$value,$tex);
    //$tex = str_replace("/\[code\]([\s\S]*?)\[\/code\]/si",$value,$tex);
}
return $tex;
//return $code;
}

$c = ubb_php("sadfaffs[*code]<b>ddd</b><span style=\"color: #0000BB\">hello,
good</span>[/*code]ddsddddfafef[*code]<span style=\"color: #0000BB\">hello,g
adffda d</span>[/*code]dfaewfadfa");
print_r($c);
var_dump($c);
[code]里的*字是为了不被处理,各位看官研究的时候自动略去。
最佳答案
评论( 相关
后面还有条评论,点击查看>>