【问题标题】:RegEX expression for images图像的正则表达式
【发布时间】:2012-09-25 13:57:23
【问题描述】:

如果<img> 标签在<script> 标签内(分别为<script language="...">,意思是<script*>),我如何修改以下正则表达式以执行

代码:

function seo_friendly_images($content) {

return preg_replace_callback('/<img[^>]+/', 'seo_friendly_images_process', $content);

}

add_filter('the_content', 'seo_friendly_images', 100);

【问题讨论】:

标签: php regex


【解决方案1】:

这在一定程度上取决于您使用的语言。某些语言支持向前或向后看(例如,仅在 bar 之后匹配 foo 或仅在 foo 之前匹配 bar)。

这对我来说看起来像 php,它报告有前瞻支持 (http://php.net/manual/en/regexp.reference.assertions.php)。

尝试使用像这样的正则表达式

    /<script[^>]+>(?=<img)/ or /<script[^>]+>(?!<img)/

?= 表示正面展望,而 ?!表示负面展望。 正向向前表示“Foo 后面跟着 Bar”,负向向前表示“Foo 后面没有 Bar”。

【讨论】:

    【解决方案2】:

    你可能想试试这个:

    return preg_replace_callback('<img[^<]+?>', 'seo_friendly_images_process', $content);
    

    虽然没有测试。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-06-11
      • 2014-12-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多