【问题标题】:regex to match p tags and add br's inside of them正则表达式匹配 p 标签并在其中添加 br
【发布时间】:2009-12-06 13:17:58
【问题描述】:

好吧,问题标题解释了问题:D,但这里仍然是一个例子

asdf
asdf
<p>asdf
asdf
asdf</p>

我如何让它对 p 标签的内部进行正则表达式并将 nl2br 应用于它,所以输出将是:

asdf
asdf
<p>asdf<br />
asdf<br />
asdf</p>

编辑:在 PHP 中

【问题讨论】:

标签: php html regex


【解决方案1】:

使用Simple HTML DOM 而不是正则表达式

$html = str_get_html(...);

foreach($html->find('p') as $element){
    //get html in p
    $ihtml = p->innertext;

    //apply function
    $ihtml = nl2br($ihtml);

    //save
    $p->innertext = $ihtml;
}

//print the new output
echo $html;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-20
    • 1970-01-01
    • 2013-08-09
    相关资源
    最近更新 更多