使用条件替换php preg_href属性
您好,我有以下标签:使用条件替换php preg_href属性,php,regex,preg-replace,Php,Regex,Preg Replace,您好,我有以下标签: $content ='<a href="http://website.com/" /> <a href="/link1" /> <a href="https://website.com" /> <a href="link1" />'; $content='1!' '; 该代码: preg_replace('~href=(\'|"|)(.*?)(\'|"|)(?<!\/|http:\/\/|ht
$content ='<a href="http://website.com/" />
<a href="/link1" />
<a href="https://website.com" />
<a href="link1" />';
$content='1!'
';
该代码:
preg_replace('~href=(\'|"|)(.*?)(\'|"|)(?<!\/|http:\/\/|https:\/\/)~i', 'href=$1http://website2.com/$2$3', $content);
preg|u replace('~href=(\'|“|”)(.*)(\'|“|”)(?提前谢谢。类似的方法应该可以。不过,我建议以后在此类任务中使用解析器。这可能会很快变得非常混乱。这里还有一个关于正则表达式用法的链接
正则表达式:
/href=("|')https?:\/\/(*SKIP)(*FAIL)|href=("|')(.*?)\2/
演示:
PHP用法:
$content ='<a href="http://website.com/" />
<a href="/link1" />
<a href="https://website.com" />
<a href="link1" />';
echo preg_replace('/href=("|\')https?:\/\/(*SKIP)(*FAIL)|href=("|\')\/?(.*?)\2/', 'href=$2http://website2.com/$3$2', $content);
演示:
PHP:
$content='1!'
';
echo preg\u replace('/href=(“\”)(?:https?:)?\/\/(*跳过)(*失败)\\ href=(“\”)\/(*?)\2/”,'href=$2http://website2.com/$3$2',$content);
输出:
<a href="http://website.com/" />
<a href="http://website2.com/link1" />
<a href="https://website.com" />
<a href="http://website2.com/link1" />
<a href="//website.com/" />
<a href="http://website2.com/link1" />
<a href="https://website.com" />
<a href="http://website2.com/link1" />
使用解析器。我已经完成了80%的工作,我的脚本只使用regex。如果您能帮助我使用regex,我将非常感激我能在您的regex中找到一些东西,在您的regex中,$1
应该是什么?$1它是用来(\'.“|)的不?谢谢,你能告诉我如何使用它吗?如果href以//开头,那么不要将iTunes更新为/
排除。更新不再被接受,答案是什么?更新有问题吗?现在我的代码可以按照我的要求为所有href工作,但是一个以一个斜杠开头的href是这样的
$content ='<a href="//website.com/" />
<a href="/link1" />
<a href="https://website.com" />
<a href="link1" />';
echo preg_replace('/href=("|\')(?:https?:)?\/\/(*SKIP)(*FAIL)|href=("|\')\/?(.*?)\2/', 'href=$2http://website2.com/$3$2', $content);
<a href="//website.com/" />
<a href="http://website2.com/link1" />
<a href="https://website.com" />
<a href="http://website2.com/link1" />