Replacing text with skip links in php [duplicate]

Good afternoon, I have a function that replaces text with clickable links from my news, but it also replaces already clickable links in the second round
how to fix?

My code

foreach ( $links as $value ) {

        $register ="";

        if ( comparehosts( urldecode($value['link']), urldecode($_SERVER['REQUEST_URI']) ) ) continue;

        if ( !$value['only_one'] ) $register .="i";
        if ( $config['charset'] == "utf-8" ) $register .= "u";
        if ( $value['targetblank'] ) $targetblank = " target="_blank""; else $targetblank = "";

        if ($value['rcount'] < 1 ) $rcount = -1; else $rcount = intval($value['rcount']);

        if ( !substr_count ($value['word'], "(") ) { 

            $find = "#(^|b|s|<br />)(" . preg_quote( $value['word'], "#" ) . ")(b|s|!|?|.|,|$)#".$register;
            $replace = "\1<a href="{$value['link']}"{$targetblank}>\2</a>\3";

        } else {

            $words = preg_quote( $value['word'], "#" );
            $words = str_replace( '|', "|", $words);
            $words = str_replace( '(', ")(", $words);
            $words = str_replace( ')', ")(", $words);

            if (substr ( $words, - 1, 1 ) == '(') $words = substr ( $words, 0, - 1 );
            if (substr ( $words, - 1, 1 ) != ')') $words .= ')';

            $words = '('.$words;

            $scount = substr_count ($words, "(");
            $rp = "";

            for ($i = 2; $i <= $scount+1; $i++) {
                $rp .= "\".$i;
            }
            
            //preg_replace('/"([A-Za-z0-9? ,_-]+)"(?=[^<>]*(?:<|$))/', "'$1'", $text);

            $find = "#(^|b|s|<br />){$words}(b|s|!|?|.|,|$)#".$register;
            $replace = "\1<a href="{$value['link']}"{$targetblank}>{$rp}</a>\{$i}";

        }


    
    
            $replace_links['all']['find'][] = $find;
            $replace_links['all']['replace'][] = $replace;
            $replace_links['all']['rcount'][] = $rcount;
    
        
    }

    unset ($links);

}

Thanks

I haven’t tried anything yet because I don’t know how