Search code examples
phpmysqlregexcodeigniterpreg-replace-callback

PHP PREG_REPLACE REGEX Mention Username


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

First off all, English isn't my native language. So, please if my grammar is bad forgive me.

For my school final project i just want to create Facebook like website (social networking). My problem is, i want to create mention feature, based on username (user database). If at Facebook after I type the @ symbol, Facebook system begin to query most possibly Friend/Page with cool ajax list display. But i don't want be like that, at my system there's no ajax list display.

If user post status update with string like @bias or @tegaralaga or @admin "@tegaralaga where are you?" for the example. My system check on database is there any user with username @tegaralaga, if yes based on user_model.php function get_fullname_by_username(); it will return user_first_name and user_last_name data. But if no it will give FALSE return. On my user table there's user with tegaralaga username, the user_first_name is Bias and user_last_name is Tegaralaga.

Move at post_helper.php, if $name==FALSE it will give current string, @tegaralaga. But if the username is exists, it will return

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

If exists, the string become

"<a href="/profile/tegaralaga.html">Bias Tegaralaga</a> where are you?"

If doesn't, the string still

 "@tegaralaga where are you?"

So my question is :
1. Is it possible with my code above using preg_replace_callback? (take a look at post_helper.php)
2. If possible, what is the perfect REGEX if we can mention more than 1 username, and the exception for email address (because email address contains @ symbol too)


Solution

  • This should work for you.. on your callback function you receive all the matches from the regexp.. you need to extract the part you need:

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