【问题标题】:PHP PREG_REPLACE REGEX Mention UsernamePHP PREG_REPLACE REGEX 提及用户名
【发布时间】:2012-03-16 22:53:06
【问题描述】:

user_model.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。但是如果用户名存在,它会返回

"{$name->user_first_name} {$name->user_last_name}"

如果存在,则字符串变为

"Bias Tegaralaga 你在哪里?

如果没有,字符串仍然

“@tegaralaga 你在哪里?”

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

【问题讨论】:

    标签: php mysql regex codeigniter preg-replace-callback


    【解决方案1】:

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

    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);
    }
    

    【讨论】:

    • 还有一个问题。像#wearetheworld 这样的哈希呢。我将正则表达式从 "#(?
    • 好吧,因为我使用 # 作为分隔符,你必须在你的正则表达式中转义它。或者像这样更改分隔符:/(?&lt;!\w)#(\w+)/
    猜你喜欢
    • 2016-09-18
    • 1970-01-01
    • 1970-01-01
    • 2012-02-20
    • 1970-01-01
    • 1970-01-01
    • 2021-02-19
    • 2017-10-24
    • 1970-01-01
    相关资源
    最近更新 更多