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

报告相同问题?

悬赏问题

  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥170 如图所示配置eNSP
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改
  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
  • ¥15 stata安慰剂检验作图但是真实值不出现在图上