gpt4 book ai didi

PHP PREG_REPLACE REGEX 提及用户名

转载 作者:行者123 更新时间:2023-11-29 02:33:14 26 4
gpt4 key购买 nike

用户模型.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 系统开始查询最有可能的 friend /页面,并显示很酷的 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> where are you?"

如果没有,字符串仍然

 "@tegaralaga where are you?"

所以我的问题是:
1. 我上面的代码可以使用 preg_replace_callback 吗? (看看 post_helper.php)
2. 如果可能的话,如果我们可以提及超过 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);
}

关于PHP PREG_REPLACE REGEX 提及用户名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9465486/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com