在数据库中检查@提及的用户,然后将其链接为用户(如果存在)

时间:2018-12-13 10:47:47

标签: php

我有这样的文字:

Hi @user1 . How are you. I am going to cinema with @user2 . 
Also his friend @user3 with us. 

好,现在usern1user2user3是用户名。 我要从数据库用户表中检查用户名是否存在。

如果提到的user1user2来自数据库,则为user1user2建立链接,但是如果user3不存在,则不存在为user3建立这样的链接:

Hi <a href="http://www.website.com/user1">@user1</a> . How are you. 
I am going to cinema with <a href="http://www.website/user2">@user2</a> . 
Also his friend @user3 with us. 

如我在示例中给出的,user1user2是已经注册的user1user2的链接。该链接未发生user3,因为user3不是注册的用户名。

我试图使它像这样:

$mentionedText = 'Hi @user1 . How are you. I am going to cinema with @user2 . Also his friend @user3 with us. ';

检查提到的用户是否存在于数据库中

public function MentionedUsers($mentionedText)
{
   $mentionedText = mysqli_real_escape_string($this->db, $mentionedText);
   preg_match_all('/@+(\w+)/u', $mentionedText, $match);
   $sql = mysqli_query($this->db, "SELECT username FROM users 
   WHERE username IN ('" . implode("','", $match[0]) . "')") 
   or die(mysqli_error($this->db));
   while($row=mysqli_fetch_array($sql)) {
        // Store the result into array
        $data[]=$row;
     }
     if(!empty($data)) {
        // Store the result into array
        return $data;
     }
}

然后我尝试输出这样的文本:

$mentionedUsersa = $Post->MentionedUsers($mentionedText); 

if($mentionedUsersa){
   foreach($mentionedUsersa as $mentin){
        $userdataname = $mentin['user_name']; 
        $dataPostTitleTextDetails = str_replace($user__name, "<a href=\"{$base_url}{$userdataname}\" class='mention_'>", $mentionedText);
   }
} 

在这次审判之后,我得到了如下输出结果。

Fatal error
: Uncaught Error: Call to undefined method POST_UPDATES::MentionedUsers()

有人在这方面可以帮助我吗?

0 个答案:

没有答案