链接人名在字符串中

时间:2015-09-08 20:08:36

标签: php

我有以下PHP函数,它自动链接人员数据库中已有的字符串中的人名。问题是字符串中已链接的名称是双重链接的。我怎样才能解决这个问题呢?

$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;

}

0 个答案:

没有答案