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="http://www.example.com/attorneys/%s">%s</a>',$url,$person),$input_string);
}
return $input_string;
}
$str = '<p>Ea velit antiopam principes sit, sit labore <a href="http://www.example.com/attorneys/john-t-smith">John T. Smith</a></p>';
echo LinkPeopleInString($str);
// output <p>Ea velit antiopam principes sit, sit labore <a href="http://www.example.com/attorneys/john-t-smith"><a href="http://www.example.com/attorneys/john-smith">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().