找到第一个换行符并替换

时间:2016-04-04 03:55:04

标签: php

我希望此代码首先找到&#34; <br>&#34;在给定的字符串上标记并替换为结束&#34; </div>&#34;标签。我无法修改促销字符串,所以我只能通过代码进行修改。我怎么做?这是我的代码: -

$promo[$i] = str_replace('<div class="well">','<div class="well"><div class="promo-subTitle">',$promo[$i]);

这是基于变量$ promo [$ i]的字符串:

$promo[1] = "<div class="promo"><div class="header">Welcome bonus for you! <br> We have a promo now! <br></div>";

2 个答案:

答案 0 :(得分:2)

您可以使用preg_replace功能执行此操作。

$promo = '<div class="promo"><div class="header">Welcome bonus for you! <br> We have a promo now! <br></div>';
$promo_out = preg_replace("/<br>/", "</div>", $promo, 1);

<强>输出:

  

HTML

<div class="promo"><div class="header">Welcome bonus for you! </div> We have a promo now! <br></div>
  

浏览器:

Welcome bonus for you!
We have a promo now! 

答案 1 :(得分:0)

$promo[$i] = preg_replace('/<br>/', '</div>', $promo[$i], 1);