简体   繁体   English

php 正则表达式匹配和替换 html a 标签排除域

[英]php regex match & replace html a tags exclude domain

I found links and replace with nofollow.我找到链接并替换为 nofollow。 But I need if "<a(.*)" line contains "google.com" stop replace.但如果“<a(.*)”行包含“google.com”,我需要停止替换。

This is my code:这是我的代码:

preg_replace('#(<a\s)#is', '\\1rel="nofollow noreferrer ugc" target="_blank" ', $post->content);

Inputs:输入:

<a href="https://www.burtinet.com/">Hosting</a>
<a href="https://www.google.com/">Search</a>

Output: Output:

<a rel="nofollow noreferrer ugc" target="_blank" href="https://www.burtinet.com/">Hosting</a>
<a rel="nofollow noreferrer ugc" target="_blank" href="https://www.google.com/">Search</a>

I need:我需要:

<a rel="nofollow noreferrer ugc" target="_blank" href="https://www.burtinet.com/">Hosting</a>
<a href="https://www.google.com/">Search</a>

try this it should do what you are looking for, I just updated your regular expression to ignore replacing if it contains google.试试这个它应该做你正在寻找的东西,我刚刚更新了你的正则表达式以忽略替换如果它包含谷歌。 we can enhance it a bit more and be specific to the href if you would like to.如果您愿意,我们可以进一步增强它并针对href

$output  = preg_replace('#((?!.*google)(<a\s))#is', '\\1rel="nofollow noreferrer ugc" target="_blank" ', $post->content);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM