douhai9043 2012-07-20 02:53
浏览 298
已采纳

使用正则表达式删除空的段落标记<p> </ p>(“空间”上的标准str_replace不起作用)

TinyMCE creates empty paragraph tags when you hit enter twice. like:

<p> </p>

Which is <p>SPACE</p>

In FireBug it calls this space a "&nbsp;" but the html code/DB backend just shows a space. When I do "str_replace('<p> </p>'....." it doesnt find the block... basically I think the "space" is somehow not a standard space and some sort of borked encoded space. Is there a regex I can run that will remove this tag? I've been stuck on this for hours... or even something like

regex('<p>LESS THAN THREE CHARS</p>'...)

would probably work

Thank you

  • 写回答

5条回答 默认 最新

  • duan198123 2012-07-20 03:00
    关注

    I would use:

    $str = preg_replace('~<p>\s*<\/p>~i','',$str);
    

    where \s signifies a white space of any kind (tab, space, etc.) and * indicates 0 or more occurence of this (space). So <p></p>, <p> </p>, <p>{multiple spaces here}</p> will all be replaced by an empty string. The additional i flag is for case-insensitivity, just in case <p>'s might instead be <P>'s.

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

报告相同问题?

悬赏问题

  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 unity第一人称射击小游戏,有demo,在原脚本的基础上进行修改以达到要求
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line