2

用户模型.php

class User_model extends CI_Model{
    function get_fullname_by_username($username){
        $query=$this->db->select('user_first_name,user_last_name')->where('user_name',$username)->get('user');
        if($query->num_rows()==0){
            return FALSE;
        } else {
            return $query->row();
        }
   }
}

post_model.php

class Post_model extends CI_Model{
    function input_post($content,$privacy==FALSE){
        $this->load->library('privacy');
        $this->load->helper('post');
        $uid=uid();//user id based on sessions
        if($privacy==FALSE){
            $privacy=$this->privacy->post_privacy($uid);
        } else {
            $privacy=$privacy;
        }
        $content=mention($content);
        $input=array('post_uid'=>$uid,'post_content'=>$content,'post_privacy'=>$privacy);
        if($this->db->insert('posts',$input)){
            return $this->fetch_single_post_data($this->db->insert_id());
        } else {
            return FALSE;
        }
    }
    function fetch_single_post_data($post_id){
        $query=$this->db->select('id,post_uid,post_content,post_privacy,post_created')->where('id',$post_id)->get('posts');
        if($query->num_rows()==0){
            return FALSE;
        } else {
            return $query->row();
        }
    }
}

post_helper.php

function get_mention_name($username){
    $username=strtolower($username);
    $CI=&get_instance();
    $CI->load->model('user_model');
    $name=$CI->user_model->get_fullname_by_username($username);
    if($name==FALSE){
        return "@".$username;
    } else {
        return "<a href=\"/profile/{$username}.html\">{$name->user_first_name} {$name->user_last_name}</a>";
    }
}

function mention($post_content){
    return preg_replace_callback("REGEX","get_mention_name",$post_content);
}

首先,英语不是我的母语。所以,如果我的语法不好,请原谅我。

对于我学校的期末项目,我只想创建类似 Facebook 的网站(社交网络)。我的问题是,我想根据用户名(用户数据库)创建提及功能。如果在 Facebook 上键入@符号后,Facebook 系统开始查询最可能的朋友/页面,并显示很酷的 ajax 列表。但我不想那样,在我的系统中没有 ajax 列表显示。

如果用户使用@bias 或@tegaralaga 或@admin 等字符串发布状态更新“@tegaralaga 你在哪里?” 例如。我对数据库的系统检查是否有任何用户名为 @tegaralaga 的用户,如果是,则基于user_model.php函数get_fullname_by_username(); 它将返回user_first_nameuser_last_name数据。但如果没有,它将返回 FALSE。在我的用户表上,有一个用户名为 tegaralaga,user_first_name 是 Bias,user_last_name 是 Tegaralaga。

移动到post_helper.php,如果 $name==FALSE 它将给出当前字符串 @tegaralaga。但是如果用户名存在,它会返回

"<a href="/profile/{$username}.html">{$name->user_first_name} {$name->user_last_name}</a>"
.

如果存在,则字符串变为

"<a href="/profile/tegaralaga.html">Bias Tegaralaga</a> 你在哪里?

如果没有,字符串仍然

“@tegaralaga 你在哪里?”

所以我的问题是:
1. 上面的代码是否可以使用 preg_replace_callback?(查看 post_helper.php)
2. 如果可能的话,如果我们可以提及超过 1 个用户名,那么完美的正则表达式是什么,以及电子邮件地址的例外(因为电子邮件地址也包含 @ 符号)

4

1 回答 1

2

这应该对你有用..在你的回调函数中,你从正则表达式中接收所有匹配项。你需要提取你需要的部分:

function get_mention_name($match){
    $username=strtolower($match[1]);
    $CI=&get_instance();
    $CI->load->model('user_model');
    $name=$CI->user_model->get_fullname_by_username($username);
    if(empty($name)){
        return "@".$username;
    } else {
        return "<a href=\"/profile/{$username}.html\">{$name->user_first_name} {$name->user_last_name}</a>";
    }
}

function mention($post_content){
    return preg_replace_callback(
        "#(?<!\w)@(\w+)#",
        "get_mention_name",
        $post_content);
}
于 2012-02-27T15:33:24.927 回答