PHP 正则替换的问题
$content = str_replace($value, $this->replaceLoop($labelInfo, $labelLoop, $nowNum, $listdb), $content);这里会把相同的[loop=1]{style}[/loop]都给替换掉,但是我希望是把数据库的数据次序替换,有什么办法么,求大神帮忙,
$content = "[loop=1]{style}[/loop][loop=1]{style}[/loop][loop=1]{style}[/loop][loop=1]{style}[/loop]";
我希望替换成$content = "$listdb[0][title]$listdb[1][title]$listdb[2][title]$listdb[3][title]";
但是现在的结果是$content = "$listdb[0][title]$listdb[0][title]$listdb[0][title]$listdb[0][title]";
程序代码:
function replaceStyleLoop($listdb, $content) { if(count($listdb) < $this->labelNum) $this->labelNum = count($listdb); if(strpos($content, "[loop") < 0) $content = "[loop={@num}]" . $content; if(!strpos($content, "[/loop]")) $content .= "[/loop]"; $content = str_replace("{@num}", $this->labelNum, $content); $nowNum = 0; preg_match_all("/\[loop=\d*][\s\S]*?\[\/loop]/", $content, $array); foreach($array[0] as $key => $value) { preg_match("/\[loop=\d*]/", $value, $arrNum); $labelLoop = (int)str_replace("[loop=", "", str_replace("]", "", $arrNum[0])); $labelInfo = str_replace("[loop=$labelLoop]", "", str_replace("[/loop]", "", str_replace("[loop]", "", $value))); $content = str_replace($value, $this->replaceLoop($labelInfo, $labelLoop, $nowNum, $listdb), $content); $nowNum += $labelLoop; if($nowNum >= $this->labelNum) break; } $content = preg_replace("/\[loop=\d*][\s\S]*?\[\/loop]/", "", $content); return $content; }
[ 本帖最后由 zhangyao3287 于 2014-4-23 17:15 编辑 ]