【问题标题】:Extend PHP regex to cover "srcset" and "style" attributes扩展 PHP 正则表达式以涵盖“srcset”和“style”属性
【发布时间】:2017-05-31 13:13:52
【问题描述】:

我创建了一个 WordPress 插件,它根据我在 $tag$attribute 变量中列出的标签和属性将所有链接转换为 protocol-relative URLs(删除 http:https:)。这是功能的一部分。为了节省空间,the rest of the code can be found here

$content_type = NULL;
# Check for 'Content-Type' headers only
foreach ( headers_list() as $header ) {
    if ( strpos( strtolower( $header ), 'content-type:' ) === 0 ) {
        $pieces = explode( ':', strtolower( $header ) );
        $content_type = trim( $pieces[1] );
        break;
    }
}
# If the content-type is 'NULL' or 'text/html', apply rewrite
if ( is_null( $content_type ) || substr( $content_type, 0, 9 ) === 'text/html' ) {
    $tag = 'a|base|div|form|iframe|img|link|meta|script|svg';
    $attribute = 'action|content|data-project-file|href|src|srcset|style';
    # If 'Protocol Relative URL' option is checked, only apply change to internal links
    if ( $this->option == 1 ) {
        # Remove protocol from home URL
        $website = preg_replace( '/https?:\/\//', '', home_url() );
        # Remove protocol from internal links
        $links = preg_replace( '/(<(' . $tag . ')([^>]*)(' . $attribute . ')=["\'])https?:\/\/' . $website . '/i', '$1//' . $website, $links );
    }
    # Else, remove protocols from all links
    else {
        $links = preg_replace( '/(<(' . $tag . ')([^>]*)(' . $attribute . ')=["\'])https?:\/\//i', '$1//', $links );
    }
}
# Return protocol relative links
return $links;

这按预期工作,但不适用于以下示例:

<!-- Within the 'style' attribute -->
<div class="some-class" style='background-color:rgba(255,255,255,0);background-image:url("http://placehold.it/300x200");background-position:center center;background-repeat:no-repeat'>
<!-- Within the 'srcset' attribute -->
<img src="http://placehold.it/600x300" srcset="http://placehold.it/500 500x, http://placehold.it/100 100w">

但是,代码部分适用于这些示例。

<div class="some-class" style='background-color:rgba(255,255,255,0);background-image:url("http://placehold.it/300x200");background-position:center center;background-repeat:no-repeat'>
<img src="http://placehold.it/600x300" srcset="//placehold.it/500 500x, http://placehold.it/100 100w">

我已经尝试向$tag$attribute 变量添加附加值,但这并没有帮助。我假设我需要更新我的正则表达式的其余部分以涵盖这两个额外的标签?还是有其他方法可以解决,比如DOMDocument

【问题讨论】:

    标签: php regex wordpress url


    【解决方案1】:

    我能够通过执行以下操作来简化代码:

    $content_type = NULL;
    # Check for 'Content-Type' headers only
    foreach ( headers_list() as $header ) {
        if ( strpos( strtolower( $header ), 'content-type:' ) === 0 ) {
            $pieces = explode( ':', strtolower( $header ) );
            $content_type = trim( $pieces[1] );
            break;
        }
    }
    # If the content-type is 'NULL' or 'text/html', apply rewrite
    if ( is_null( $content_type ) || substr( $content_type, 0, 9 ) === 'text/html' ) {
        # Remove protocol from home URL
        $website = $_SERVER['HTTP_HOST'];
        $links = str_replace( 'https?://' . $website, '//' . $website, $links );
        $links = preg_replace( '|https?://(.*?)|', '//$1', $links );
    }
    # Return protocol relative links
    return $links;
    

    【讨论】:

    • 警告,它可能会替换以文本形式出现的链接,例如:“我的网站是 example.com”将变成“我的网站是 //example.com/”。它还会破坏一些必须具有 http(s) 的标签,例如 og:url
    猜你喜欢
    • 2013-03-16
    • 2016-12-02
    • 2020-05-24
    • 2016-09-26
    • 1970-01-01
    • 1970-01-01
    • 2010-11-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多