With preg_match it's possible to have the same pattern and have different replacements? : <p>I create this pattern </p>

<pre><code>$pattern = "/&lt;a href='(?&lt;href&gt;.+?)'&gt;(?&lt;name&gt;.+?)&lt;\/a&gt;/i";
</code></pre>

<p>And i have this example,</p>

<pre><code>$string = "&lt;a href='https://www.php.net/'&gt;https://www.php.net/&lt;/a&gt; 
&lt;a href='https://stackoverflow.com/'&gt;https://stackoverflow.com/&lt;/a&gt; 
&lt;a href='https://www.google.com/'&gt;https://www.google.com/&lt;/a&gt;";
</code></pre>

<p>Using this, i can find the matches and extract the href, and the name. </p>

<pre><code>preg_match_all($pattern, $string, $matches);

Array
(
    [0] =&gt; Array
        (
            [0] =&gt; https://www.php.net/
            [1] =&gt; https://stackoverflow.com/
            [2] =&gt; https://www.google.com/
        )

    [href] =&gt; Array
        (
            [0] =&gt; https://www.php.net/
            [1] =&gt; https://stackoverflow.com/
            [2] =&gt; https://www.google.com/
        )

    [1] =&gt; Array
        (
            [0] =&gt; https://www.php.net/
            [1] =&gt; https://stackoverflow.com/
            [2] =&gt; https://www.google.com/
        )

    [name] =&gt; Array
        (
            [0] =&gt; https://www.php.net/
            [1] =&gt; https://stackoverflow.com/
            [2] =&gt; https://www.google.com/
        )

    [2] =&gt; Array
        (
            [0] =&gt; https://www.php.net/
            [1] =&gt; https://stackoverflow.com/
            [2] =&gt; https://www.google.com/
        )

)
</code></pre>

<p>The problem is when I use the preg_replace, since the pattern is the same, it changes the same information for all the URL, and i need to change only the name and preserve the rest of the information accordingly.</p>

<p>Using,</p>

<pre><code>if(preg_match_all($pattern, $string, $matches))
{
    $string = preg_replace($pattern, "&lt;a href='$1'&gt;Name&lt;/a&gt;", $string);

}
</code></pre>

<p>I can get the results from the groups, and preserve the first part of the href. But if i try to change the name, it's the same for all results. </p>

<p>If I try to use "str_replace", I can have different results as intended, but this gave me 2 problems. One is that if i try to replace the name, i also change the href, and if i have similar URL with "more slashes" it will change the match part, and leave the rest of the information.</p>

<p>In a database I have list of URL with a column that have names, and if the string match any row in the table I need to change the name accordingly and preserve the href. </p>

<p>Any help? </p>

<p>Thank you.</p>

<p>Kind regards!</p>
