我想替换PHP中<br />
标记之间的<pre></pre>
标记。
像这样:
$content = "Hello,<br /> <-- don't replace
Line 1<br /> <-- don't replace
Line 2<br /> <-- don't replace
<pre>Line 3<br /> <-- replace
Line 4</pre>";
我该怎么做?
答案 0 :(得分:0)
function get_string($string, $start, $end){
$string = " ".$string;
$pos = strpos($string,$start);
if ($pos == 0) return "";
$pos += strlen($start);
$len = strpos($string,$end,$pos) - $pos;
$changeString = str_replace("<br />", "<replacement>", substr($string,$pos,$len) );
// above line replace <br /> data with <replacement>
return substr($string,0,$pos).$changeString.substr($string,$pos+$len);
}
$parsed = get_string($content , "<pre>", "</pre>");
echo $parsed;
我很困惑,因为您帖子中的$ content中没有 pre 标记... 只有我看到代码标签