duanmiaosi0150 2014-06-25 10:46
浏览 46
已采纳

preg_replace仅删除结束标记

I'm working on a joomla site that use JotCache as cache component. To exclude from cache some modules directly on template files, this component use own "markers" such as:

<jot myposition s> Module Position <jot myposition e>

Now, i'm trying to minify html trough php using DOMDocument but the result is this and the cache component doesn't work:

<jot myposition s> Module Position <jot myposition e></jot></jot>

I'm thinking to use preg_replace to strip the </jot> closing tag. I tried this regex "/<[\/]*jot[^>]*>/i" but it strips all <jot> tags, including the required <jot myposition s> and <jot myposition e>.

Since I'm not sure how to accomplish this with DOMDocument (prevent tags closing automatically), how can I do this with preg_replace?

Any ideas would be very appreciated.

Thanks.

  • 写回答

3条回答 默认 最新

  • dshfjsh_5455 2014-06-25 10:49
    关注

    A Nice Chance to Explore some Regex Features!

    With all the disclaimers about using regex to work with xml-type documents... There are several interesting options for such a task.

    Option 1: Plain but Reliable

    $replaced = preg_replace('%(<jot.*?</jot>)</jot>%', '$1', $yourstring);
    
    • Here, for safety, we match your whole string including the two </jot> at the end.
    • The .*? "lazy dot-star" quantifier ensures we don't accidentally run past the first closing </jot>
    • The parentheses capture the string you want to Group 1
    • We replace with Group 1

    Option 2: More "Cheeky"

    $replaced = preg_replace('%</jot></jot>%', '</jot>', $yourstring);
    
    • Here, we just match </jot></jot>
    • We replace with </jot>

    Option 3: Futuristic

    $replaced = preg_replace('%</jot>(?=</jot>)%', '', $yourstring);
    
    • Here, we match </jot>, then the lookahead (?=</jot>) asserts that </jot> can be found again, but doesn't match it.
    • We replace with an empty string

    Option 4: Keep Out!

    $replaced = preg_replace('%<jot.*?</jot>\K</jot>%', '', $yourstring);
    
    • As in the first option, <jot.*?</jot> matches a whole tag...
    • Then \K tells the engine to drop whatever has been matched so far!
    • and </jot> matches the second </jot>
    • which we replace with the empty string
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥20 关于URL获取的参数,无法执行二选一查询
  • ¥15 液位控制,当液位超过高限时常开触点59闭合,直到液位低于低限时,断开
  • ¥15 marlin编译错误,如何解决?
  • ¥15 有偿四位数,节约算法和扫描算法
  • ¥15 VUE项目怎么运行,系统打不开
  • ¥50 pointpillars等目标检测算法怎么融合注意力机制
  • ¥20 Vs code Mac系统 PHP Debug调试环境配置
  • ¥60 大一项目课,微信小程序
  • ¥15 求视频摘要youtube和ovp数据集
  • ¥15 在启动roslaunch时出现如下问题