Check if text is already linked?

I have the following PHP function which automatically links people’s names in a string. It works great, but if the person’s name is already linked then it double links. How can I get around this problem?

function LinkPeopleInString($input_string){

    $people_array = array('john-smith' => 'John T. Smith', 'jane-doe' => 'Jane F. Doe'); // array of attorneys that could potentially be linked

    foreach($people_array as $url => $person){
        $input_string = str_replace($person, sprintf('<a href="">%s</a>',$url,$person),$input_string);

    return $input_string;


$str = '<p>Ea velit antiopam principes sit, sit labore <a href="">John T. Smith</a></p>';

echo LinkPeopleInString($str);

// output <p>Ea velit antiopam principes sit, sit labore <a href=""><a href="">John T. Smith</a></a></p>

What you could possibly do is, within the foreach iteration, have a regular expression verify if $person is surrounded by an anchor tag with a non-empty href attribute. If you want, you can even grab the value of href and verify that it’s a valid URL by using PHPs filter_var().

There are several ways to handle it.

1: Regular Expression from the Start. (Recommended)

$input_string = preg_replace("~".$person."(?!</a>)~","<a href='$url'>$person</a>",$input_string)

2: Regular expression cleanup.
After running your loop:

$input_string = preg_replace("~(<a .*?>){2}(.*?)(</a>){2}~","$1$2$3",$input_string);

(Note that this will discard the outer URL, and keep the inner one)

StarLion - Your recommended solution worked great. Thank you!!!

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.