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

正则表达式:如果内部没有数据,则将<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 matlab图像高斯低通滤波
  • ¥15 针对曲面部件的制孔路径规划,大家有什么思路吗
  • ¥15 钢筋实图交点识别,机器视觉代码
  • ¥15 如何在Linux系统中,但是在window系统上idea里面可以正常运行?(相关搜索:jar包)
  • ¥50 400g qsfp 光模块iphy方案
  • ¥15 两块ADC0804用proteus仿真时,出现异常
  • ¥15 关于风控系统,如何去选择
  • ¥15 这款软件是什么?需要能满足我的需求
  • ¥15 SpringSecurityOauth2登陆前后request不一致
  • ¥15 禅道二次开发编辑版本,上传不了发行包