Cloudflare插件preg_replace改进

时间:2016-07-06 09:27:41

标签: php preg-replace

我是PHP的新手,如果我的问题看起来很幼稚,请提前抱歉。

有一个插件(实际上非​​常流行的Cloudflare SSL for Wordpress),用于将http链接转换为https,它看起来像:

之前:

<link rel="alternate" type="application/rss+xml" title="A B &raquo; Feed" href="http://site.biz/feed/" />

后:

<link rel="alternate" type="application/rss+xml" title="A B &raquo; Feed" href="//site.biz/feed/" />

这是一个执行此操作的代码:

 if (is_null($content_type) || substr($content_type, 0, 9) === 'text/html') {
    // replace href or src attributes within script, link, base, and img tags with just "//" for protocol
    $re     = "/(<(script|link|base|img|form)([^>]*)(href|src|action)=[\"'])https?:\\/\\//i";
    $subst  = "$1//";
    $return = preg_replace($re, $subst, $buffer);

除了这个插件无法处理这样的链接之外,一切都很好:

<div  class="dslc-modules-section " style="background-color:rgb(14, 134, 219);background-image:url();background-image:url(http://site.biz/wp-content/uploads/2016/07/CloudPatches.jpg);background-position:center center;background-size:cover;padding-bottom:0px;padding-top:0px;">

有没有人知道如何改进这部分代码以使其更完美?

1 个答案:

答案 0 :(得分:0)

删除http:

background-image:url(/wp-content/uploads/2016/07/CloudPatches.jpg)