简体   繁体   English

正则表达式以匹配p标签并在其中添加br

[英]regex to match p tags and add br's inside of them

Well the question title explains the question :D but here is an example none the less 问题标题说明了问题:D,但这仍然是一个示例

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

how to i get it to regex the inside of the p tags and apply nl2br to it so the output would be: 我如何获取它来对p标签内部进行正则表达式并对其应用nl2br,因此输出将是:

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

Edit: In PHP 编辑:在PHP

Use Simple HTML DOM rather than regex 使用简单的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;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM