【问题标题】:PHP replace between pre tags [duplicate]PHP在pre标签之间替换[重复]
【发布时间】:2013-11-01 17:27:39
【问题描述】:

我想替换 PHP 中 <pre></pre> 标记之间的 <br /> 标记。 像这样:

$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>";

我该怎么做?

【问题讨论】:

  • 没有在 $content 中找到任何 pre 标签,或者您想替换 标签之间的 br 标签
  • @Simon911 用什么替换
  • @Bigood:尝试to bring about the end of times
  • 糟糕,对不起,我的意思是 $content 中的
     标签

标签: php replace


【解决方案1】:
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 标签... 只有我看到的是代码标签

【讨论】:

  • @Simon911 自己试试吧!...它会产生所需的结果!
  • 是的,谢谢,它有效!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-02-07
  • 1970-01-01
  • 1970-01-01
  • 2019-10-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多