我在WordPress中运行了一个PHP脚本,它从所有链接( 这是完整的代码 )中删除http:和https:协议,但是下面是剥离协议的部分:
# If 'Relative' option is selected, remove domain from all internal links
if ( $this->option == 2 ) {
$website = preg_replace( '|https?:\/\/|', '', home_url() );
$links = preg_replace( '|https?:\/\/' . $website . '|', '', $links );
}
# For all external links, remove protocols
$links = preg_replace( '|https?:\/\/|', '', $links );这是按计划进行的。但是,它还删除了文本字段中的协议,如下所示:

我试图更新regex以忽略任何<input>字段,这样它就不会删除协议。特别是带有value=http:或value=https: 从我的测试中的
<input type="url" value="http://example.com">我想出了一个模式:
<input(.*)value=["\']https?:\/\/但我不知道如何实现这个正则表达式,以跳过任何与此逻辑匹配的<input>字段。我尝试了以下逻辑,但没有奏效:
# Skip input fields with links
if ( preg_match( '|<input(.*)value=["\']https?:\/\/|', $links ) ) {
return $links;
}
# If 'Relative' option is selected, remove domain from all internal links
if ( $this->option == 2 ) {
$website = preg_replace( '|https?:\/\/|', '', home_url() );
$links = preg_replace( '|https?:\/\/' . $website . '|', '', $links );
}
# For all external links, remove protocols
$links = preg_replace( '|https?:\/\/|', '//', $links );发布于 2017-03-27 17:52:56
多亏了Wiktor Stribiżew,这是一个工作正则表达式:
/<input\b[^<]*\bvalue=[\"\']https?:\/\/(*SKIP)(*F)|https?:\/\//https://stackoverflow.com/questions/43020468
复制相似问题