帮助正则表达式 - 我怎么能让一些网址不跟随?

Har*_*ldo 5 javascript regex nofollow

好的,所以我已经使这个功能适用于将大多数网址如pies.com或www.cakes.com转换为实际的链接标记.

function render_hyperlinks($str){       
    $regex = '/(http:\/\/)?(www\.)?([a-zA-Z0-9\-_\.]+\.(com|co\.uk|org(\.uk)?|tv|biz|me)(\/[a-zA-Z0-9\-\._\?&=#\+;]+)*)/ie';    
    $str = preg_replace($regex,"'<a href=\"http://www.'.'$3'.'\" target=\"_blank\">'.strtolower('$3').'</a>'", $str);
    return $str;    
}
Run Code Online (Sandbox Code Playgroud)

我想更新此功能,no-follow为我的竞争对手的链接添加标签,

所以我会有一些关键词(竞争对手的名字)nofollow例如,如果我的网站是关于烘焙我可能想:

no-follow any sites with the phrases 'bakingbrothers', 'mrkipling', 'lyonscakes'
Run Code Online (Sandbox Code Playgroud)

是否有可能将此实现if(contains x){ add y}到我的正则表达式?

这就是所谓的"回顾"吗?

Nar*_*adu 2

也许 preg_replace_callback 就是您正在寻找的:

function link($matches)
{
    $str_return = '<a href="http://www.'.$matches[3].'" target="_blank"';
    if(in_array($matches[3], $no_follow_array))
    {
        $str_return .= ' no-follow';
    }
    $str_return .='>'.strtolower($matches[3]).'</a>';
}

$regex = '/(http:\/\/)?(www\.)?([a-zA-Z0-9\-_\.]+\.(com|co\.uk|org(\.uk)?|tv|biz|me)(\/[a-zA-Z0-9\-\._\?&=#\+;]+)*)/ie';    
$str = preg_replace_callback($regex,'link', $str);
Run Code Online (Sandbox Code Playgroud)