PHP只从函数中提取用户,并使用forch插入db notify表
我的函数从文本中提取@users、#hashtags链接和表情符号PHP只从函数中提取用户,并使用forch插入db notify表,php,regex,function,for-loop,foreach,Php,Regex,Function,For Loop,Foreach,我的函数从文本中提取@users、#hashtags链接和表情符号 function convert_text($str) { $regex = "/[@#](\w+)/"; //type and links $hrefs = [ '#' => 'hashtag.php?hashtag', '@' => 'user.php?user'
function convert_text($str) {
$regex = "/[@#](\w+)/";
//type and links
$hrefs = [
'#' => 'hashtag.php?hashtag',
'@' => 'user.php?user'
];
$result = preg_replace_callback($regex, function($matches) use ($hrefs) {
return sprintf(
'<a href="%s=%s">%s</a>',
$hrefs[$matches[0][0]],
$matches[1],
$matches[0]
);
}, $str);
//$result = preg_replace("/U\+([A-F0-9]{5})/", '\u{${1}}', $result);
$result = preg_replace('/U\+([A-F0-9]{5})/', '<span style="font-size:30px;">&#x\\1;</span>', $result);
return ($result);
}
也许吧
foreach($user_found as $user => $u){
//execute query
}
示例:
text here@user1 test@user2
提取@user1、@user2
并在数据库中插入找到的每个用户的foreach
解决这个问题的最好办法是什么
PDO示例
function update_notifications($str) {
global $conexao_pdo;
if (preg_match_all("/@(\w+)/", $str, $matches)) {
$query = "INSERT INTO notification (from_who, type, to_user, time, notification_seen) VALUES (:from_who, :type, :to_user, :time, :notification_seen)";
$notifications = array();
foreach ($matches[1] as $user) {
//$notifications[] = "('$user', '$user found in \'$str\'')";
$notifications = "('$user')";
}
$query .= implode(',', $notifications);
}
// execute query
//echo $query;
$notification_seen = "no";
$l = "like";
$time = date('d-m-Y G:i:s');
$notify = $conexao_pdo->prepare($query);
$notify->bindParam(':from_who', $user_logged);
$notify->bindParam(':type', $l);
$notify->bindParam(':to_user', $notifications);
$notify->bindParam(':time', $time);
$notify->bindParam(':notification_seen', $notification_seen);
$notify->execute();
}
update_notifications('text here @user1 test @otaviobarreto');
您可以通过在php中创建一个简单的函数来获得所需的结果。另外,这里我在字符串中添加了一个额外的空格来获取最后一个子字符串
<?php
$string = "text here @user1 test @user2";
$string .= " ";
$result = getStrings($string, '@', ' ');
//print result here
print_r($result);
//pass string, string starting char and end char here
function getStrings($string, $start, $end)
{
$pattern = sprintf(
'/%s(.*?)%s/',
preg_quote($start),
preg_quote($end)
);
preg_match_all($pattern, $string, $matches);
return $matches[1];
}
?>
输出:
排列(
[0]=>user1
[1] =>用户2)
您可以使用如下函数根据在给定字符串中找到的用户生成和执行查询。它用于查找字符串中命名的所有用户
function update_notifications($str) {
if (preg_match_all("/@(\w+)/", $str, $matches)) {
$query = "INSERT INTO notify (from_user, notification) VALUES ";
$notifications = array();
foreach ($matches[1] as $user) {
$notifications[] = "('$user', '$user found in \'$str\'')";
}
$query .= implode(',', $notifications);
}
// execute query
echo $query;
}
update_notifications('text here @user1 test @user2');
对于示例字符串,这将生成以下查询:
INSERT INTO notify (from_user, notification)
VALUES ('user1', 'user1 found in \'text here @user1 test @user2\''),
('user2', 'user2 found in \'text here @user1 test @user2\'')
更新
根据问题中更新的代码,我认为这应该可以工作,尽管如果没有访问数据库的权限,我不可能进行测试
function update_notifications($str) {
global $conexao_pdo, $user_logged;
$query = "INSERT INTO notification (from_who, type, to_user, time, notification_seen) VALUES (:from_who, :type, :to_user, :time, :notification_seen)";
$notify = $conexao_pdo->prepare($query);
$notify->bindParam(':from_who', $user_logged);
$notify->bindParam(':type', $l);
$notify->bindParam(':to_user', $user);
$notify->bindParam(':time', $time);
$notify->bindParam(':notification_seen', $notification_seen);
$notification_seen = "no";
$l = "like";
$time = date('d-m-Y G:i:s');
if (preg_match_all("/@(\w+)/", $str, $matches)) {
foreach ($matches[1] as $user) {
// execute query
$notify->execute();
}
}
}
update_notifications('text here @user1 test @user2');
您可以显示一些示例输入数据和预期输出吗?例如:
text here@user1 test@user2
extract@user1、@user2
并使用foreach user
found@OtávioBarreto刚刚尝试了一个简单的解决方案,以得到您想要使用PDO的结果,应该也使用PDO,对吗?我只需要将pdo变量传递到函数中。@Otáviobarre是的,只需将pdo变量传递到函数中,然后使用类似于$result=$pdo->query($query)的东西代码>我用我需要的PDO bindParam更新了这个问题,我仍在试图弄清楚如何使用它来查看我的编辑。“我认为它应该非常接近你所需要的东西。”奥塔维奥巴雷托说。我很高兴能帮上忙。
function update_notifications($str) {
global $conexao_pdo, $user_logged;
$query = "INSERT INTO notification (from_who, type, to_user, time, notification_seen) VALUES (:from_who, :type, :to_user, :time, :notification_seen)";
$notify = $conexao_pdo->prepare($query);
$notify->bindParam(':from_who', $user_logged);
$notify->bindParam(':type', $l);
$notify->bindParam(':to_user', $user);
$notify->bindParam(':time', $time);
$notify->bindParam(':notification_seen', $notification_seen);
$notification_seen = "no";
$l = "like";
$time = date('d-m-Y G:i:s');
if (preg_match_all("/@(\w+)/", $str, $matches)) {
foreach ($matches[1] as $user) {
// execute query
$notify->execute();
}
}
}
update_notifications('text here @user1 test @user2');