PHP: How to insert a string into matched regex pattern (adding rel="no-follow" to anchor links)
I am writing a commenting system for my website, using PHP.

I want to do the following:

1. Detect all external links (i.e. anchor tags with source NOT containing the string mywebsite.com) in a comment
2. Add the string 'rel="no-follow"' to anchor tags identified in step 1 above.

I have an idea for such a function, but I will need some help from more experienced PHP developers so that I'm sure I'm doing things the right way. This is what my first attempt looks like

    <?php
    
    function process_comment($comment)
    {
    
        $external_url_pattern = "href=[^mywebsite.com]"; //this regex is probably wrong (Help!)
    
        //are there any matches
        $matches = array();
        preg_match_all($external_url_pattern, $comment, $matches);
    
        foreach($matches as $match)
        {
           // how do we insert the 'rel="no-follow" string ?
        }
    
    }
    
    
    ?>

Would appreciate any comments, pointers and tips in helping me complete this function. Thanks.
PHP: How to insert a string into matched regex pattern (adding rel="no-follow" to anchor links)
===

I am writing a commenting system for my website, using PHP.

I want to do the following:

1. Detect all external links (i.e. anchor tags with source NOT containing the string mywebsite.com) in a comment
2. Add the string 'rel="no-follow"' to anchor tags identified in step 1 above.

I have an idea for such a function, but I will need some help from more experienced PHP developers so that I'm sure I'm doing things the right way. This is what my first attempt looks like

    <?php
    
    function process_comment($comment)
    {
    
        $external_url_pattern = "href=[^mywebsite.com]"; //this regex is probably wrong (Help!)
    
        //are there any matches
        $matches = array();
        preg_match_all($external_url_pattern, $comment, $matches);
    
        foreach($matches as $match)
        {
           // how do we insert the 'rel="no-follow" string ?
        }
    
    }
    
    
    ?>

Would appreciate any comments, pointers and tips in helping me complete this function. Thanks.