duan01203 2011-03-08 12:53
浏览 31

正则表达式提取没有特定参数的HTML标记(修复WordPress插件)

I need the help of some RegEx-Experts to fix a bug in a WordPress-Plugin, which is no longer maintained by the author.

Inside the plugin there is the following php-sytax to find included scripts:

'/(\\s*)(<script\\b[^>]*?>)([\\s\\S]*?)<\\/script>(\\s*)/i'

This line filters scripts no matter for what media they are written. To fix an bug this line must be changed, so that script tags with the parameter media="print" are not extracted.

How must this line be chanced that script tags with parameter media="print" are not affected?

See here for the topic in the WordPress-Support-Forum.

  • 写回答

3条回答 默认 最新

  • drymoeuka282427675 2011-03-08 13:04
    关注

    preg are not meant to match HTML tags. You'll never know where and how attributes will be defined :

    <script media="print">
    <script media=print>
    <script type="text/javascript" media="print">
    <script media="print" type="text/javascript">
    

    Basically, you cannot handle that in a good way with pregs. I'd suggest you to extract the html you want to clean into some DOM (or even SimpleXML) object and get all script tags where attributes are "print" with an xpath function

    //script[media="print"]
    
    评论

报告相同问题?

悬赏问题

  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示