2

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 のような Web サイト (ソーシャル ネットワーキング) を作成したいと考えています。私の問題は、ユーザー名(ユーザーデータベース)に基づいてメンション機能を作成したいということです。Facebook で@記号を入力すると、Facebook システムは、クールな ajax リスト表示を使用して、おそらく友人/ページのクエリを開始します。しかし、私はそのようになりたくありません。私のシステムには ajax リスト表示がありません。

ユーザーが @bias または @tegaralaga または @admin "@tegaralaga where are you?" のような文字列でステータス更新を投稿した場合 例として。データベースの私のシステムチェックは、ユーザー名 @tegaralaga を持つユーザーがいるかどうかを確認しますuser_first_nameおよびuser_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. 可能であれば、複数のユーザー名に言及できる場合の完璧な REGEX と、電子メール アドレスの例外 (電子メール アドレスには @ 記号も含まれているため) を教えてください。

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 に答える