Hello there,

I got forum (phpbb3) and i have topics with a lot of links, like this:
<a href="link..." class="postlink">Link name...</a>

in every link i have class="postlink" and i want to add rel="nofollow" for every external link except my domain by check the 'href' definition.
Its possible with only preg_replace with callback and how ?

Example:
<a href="http://google.com" class="postlink">Link name...</a>
<a href="http://mysite.com" class="postlink">Link name...</a>
i want to be
<a href="http://google.com" class="postlink" rel="nofollow">Link name...</a>
<a href="http://mysite.com" class="postlink">Link name...</a>

Thanks for any help!

$result = preg_replace(
    '%<a href="(?!http://mysite\.com)(.*?)" class="postlink">%', 
    '<a href="$1" class="postlink" rel="nofollow">', 
    $subject);

It works, but i have http:// and https:// links, can you edit to except only 'mysite.com'
Thanks!

Just add s?

Im unfamiliar with regular expressions and i dont know in which place to add this.
Sorry but, can you give me full code with this 's?'.

edit:
i have https:// http:// and https://www

https?://

Yeah i gotcha, but i have https://www, how to escape and them ?
I want only the domain name like 'mysite.com'.

(?!https?://(www\.)?mysite\.com)

Replace needs to use $2 then, instead of $1

Not works in that way.
I made this:

        $message = preg_replace(
        '%<a href="(?!https?://(www\.)?site\.com)" class="postlink">%', 
        '<a href="$2" class="postlink" rel="nofollow" target="_blank">', 
        $message);

But not working.

$message = preg_replace(
    '%<a href="(?!https?://(www\.)?site\.com)(.*?)" class="postlink">%', 
    '<a href="$2" class="postlink" rel="nofollow" target="_blank">', 
    $message);

You omitted a part.

Oh... I see..
Now its working, thank you!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.