网页学习体会

  • 首页
  • 个人博客
您的位置: 首页  >  技术问答  >  正则表达式:如果内部没有数据,则将
标签的内容替换为
标签

正则表达式:如果内部没有数据,则将
标签的内容替换为
标签

分类: 技术问答 • 2022-02-28 18:22:38

正则表达式:如果内部没有数据,则将<div>标签的内容替换为<br>标签

问题描述:

Code:

    <div>
      <font face="Arial, Verdana">
         <span style="font-size: 13.3333px;">
           <u>
             Hello World
           </u>
       </span>
    </font>
 </div>
    <div>
      <font face="Arial, Verdana">
         <span style="font-size: 13.3333px;">
           <u>
            Hello World2
           </u>
            <br>
       </span>
    </font>
 </div>
 <div>
    <br>
 </div>
 <div>
      <font face="Arial, Verdana">
        <span style="font-size: 13.3333px;">
           <u>
               <br>
            </u>
        </span>
      </font>
 </div>

Output:- I wish to get exactly the same output as given below

    <div>
      <font face="Arial, Verdana">
         <span style="font-size: 13.3333px;">
            <u>
             Hello World
            </u>
          </span>
       </font>
     </div>
    <div>
      <font face="Arial, Verdana">
         <span style="font-size: 13.3333px;">
           <u>
            Hello World2
           </u>
            <br>
       </span>
    </font>
 </div>
 <br>
 <br>

Here is what I have tried:

$html = preg_replace("/<div.*?>.*?<br.*?>.*?<\/div>/", "", $html);

but it is not working fine.

Please take a look on the code and suggest me how I can replace "div" tag from its starting to corresponding closing tag with "br" tag only when there is no text is present under "div" tag. As shown above in second "div" tag.

答

I would highly suggest using DOM Manipulation to accomplish this. You can use regular expressions and you can make other solutions work. However, DOM Manipulation was created for this exact reason.

There are many examples of DOM Parsers in PHP. Some are slower than others. Check out this SO post for a great listing of potential candidates for DOM Parsers.

You could always use Regular Expressions - but here's the condition under which I would personally use a Regular Expression: If you never plan to add any other functionality to this. If you do plan to add more, change it up, make your script more versatile, etc., then I'd say don't use a RegEx. The reason is you will either end up with a huge, completely daunting expression, or you'll end up with many small "one-off" expressions. It will take you less time to reference a function inside a DOM Parser than it will to figure out the proper Regular Expression.

EDIT:

I've removed my code snippet - Splash58's answer is a more elegant solution. His example uses native PHP which 9.9 times out of 10 is better (quicker, more efficient, more community support, etc).

答

use domDocument to manipulate with html structure

$doc = new DOMDocument();
$doc->loadHtml($pageHtml);
$x = new DOMXpath($doc);
foreach($x->query('//div[normalize-space(.) = ""]') as $div) {
    $link= $doc->createElement('br');
    $div->parentNode->replaceChild($link, $div);
}

echo $doc->saveHTML();

demo

相关推荐

  • 正则表达式:如果内部没有数据,则将
    标签的内容替换为
    标签
  • 正则表达式 匹配
  • Visual Studio Team Foundation Server 2015 Dash Board
    网站免责声明 网站地图 最新文章 用户隐私 版权申明
本站所有数据收集于网络,如果侵犯到您的权益,请联系网站进行下架处理。   

Copyright © 2018-2021   Powered By 网页学习体会    备案号:   粤ICP备20002247号