I have the following PHP function which automatically links people's names in a string that are already in the people database. The problem is names that are already linked in the string are double linked. How can I get around this problem?
$str = '<a href="">John T. Smith.</a>';
echo LinkPeopleInString($str);
output <a href=""><a href="">John T. Smith.</a></a>
function LinkPeopleInString($input_string){
$connector = $this->DB();
$sql = $connector->query("SELECT person_url_name, person_first, person_middle, person_last, person_suffix
FROM people WHERE person_draft = 0 AND person_active = 1");
if(!$sql){
return $input_string;
}
$people_array = array();
while($row = $connector->fetchArray($sql)){
$person_url_name = $row['person_url_name'];
$people_array[$person_url_name] = $this->FormatName($row['person_first'], $row['person_middle'], $row['person_last'], $row['person_suffix']);
}
foreach($people_array as $url => $person){
$input_string = str_replace($person, sprintf('<a href="'.SITE_URL.'/'.PEOPLE_DIRECTORY.'/%s">%s</a>',$url,$person),$input_string);
}
return $input_string;
}