douguanci9158 2016-04-29 16:11
浏览 160
已采纳

正则表达式:如果内部没有数据,则将<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.

  • 写回答

2条回答 默认 最新

  • duanan5940 2016-04-29 16:41
    关注

    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).

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀