用户模型.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_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> where are you?"
如果没有,字符串仍然
"@tegaralaga where are you?"
所以我的问题是:
1.我上面的代码是否可以使用preg_replace_callback? (看看 post_helper.php)
2.如果可能的话,如果我们可以提及超过1个用户名,那么完美的正则表达式是什么,以及电子邮件地址的异常(exception)(因为电子邮件地址也包含@符号)
最佳答案
这应该适合你..在你的回调函数中,你收到来自正则表达式的所有匹配项..你需要提取你需要的部分:
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);
}
关于PHP PREG_REPLACE REGEX 提及用户名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9465486/