dsj8086 2013-03-29 17:36
浏览 41
已采纳

如何仅删除图像宽度和高度,而不是iframe? 代码包括在内

The code I'm currently using in my wordpress functions file is this:

function remove_width_attribute( $html ) {
   $html = preg_replace( '/(width|height)=("|\')\d*(|px)("|\')\s/', "", $html );
   return $html;
}
add_filter( 'the_content', 'remove_width_attribute', 10 );

The problem here is that it targets iframes too, so how would I rewrite the regex to say images only?

  • 写回答

2条回答 默认 最新

  • douyong1285 2013-03-29 17:59
    关注

    I would just use non capturing groups to search for the img tag before the other statements, something like

    (?:img.*)(width|height ?= ?['"].*['"])

    Which you can see working here: http://regex101.com/r/mI1bD5.

    Note that this fails in a lot of simple cases (iframe on the same line as an img tag for example), but it should push you in the right direction.

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

报告相同问题?

悬赏问题

  • ¥15 全志v3s怎么设置高速时钟,使用的荔枝派zero开发板,串口2需要921600的波特率
  • ¥15 关于#单片机#的问题:Lora通讯模块hc-14电路图求内部原理图
  • ¥50 esp32 wroom 32e 芯片解锁
  • ¥15 bywave配置文件写入失败
  • ¥20 基于Simulink的ZPW2000轨道电路仿真
  • ¥15 pycharm找不到在环境装好的opencv-python
  • ¥15 在不同的执行界面调用同一个页面
  • ¥20 基于51单片机的数字频率计
  • ¥50 M3T长焦相机如何标定以及正射影像拼接问题
  • ¥15 keepalived的虚拟VIP地址 ping -s 发包测试,只能通过1472字节以下的数据包(相关搜索:静态路由)