duanjiaoxi4928 2013-01-04 21:21
浏览 53

用php(或python)删除<li>标签之间的html换行符

I have a large data set of html text, and I frequently find unnecessary, and sometimes multiple, <br> line breaks within <li> tags.

For example:

<li>Some string here<br></li><br><li>Another string here<br><br></li><br>

I would like to remove these <br> that appear between <li> and </li> but preserve everything else, including <br> outside of <li> tags. The text above would become:

<li>Some string here</li><br><li>Another string here</li><br>

What is the regular expression for doing this with preg_replace() in php (or re.sub() in python)?

  • 写回答

2条回答 默认 最新

  • duanfeigui6655 2013-01-04 21:28
    关注

    replacing (<br>)+</li> with </li> will take care of line breaks at the end of the li content at least, which may be good enough for you. Otherwise, as <li>s sometimes contain other <li>s, you may have a pretty difficult task in front of you that cannot easily be solved with regexp (and maybe cannot be solved with regexp alone at all), see the accepted answer to this question.

    评论

报告相同问题?

悬赏问题

  • ¥15 关于#网络安全#的问题:求ensp的网络安全,不要步骤要完成版文件
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥20 使用Photon PUN2解决游戏得分同步的问题
  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM