【问题标题】:PHP extract only users from function and insert into db notify table with forechPHP仅从函数中提取用户并使用foreach插入db通知表
【发布时间】:2019-12-31 05:19:26
【问题描述】:

我的函数从文本中提取@users、#hashtags 链接和表情符号

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

我想创建另一个函数来提取仅@users和 插入notifyforeach @user 在文本中找到 查询类似于:

$query = "INSERT INTO notify ('from_user', 'notification') VALUES ('$users_found)

也许

foreach($user_found as $user => $u){
    //execute query 
}

例如:text here @user1 test @user2

提取@user1, @user2

并使用foreach user found 插入数据库

解决这个问题的最佳方法是什么?

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

【问题讨论】:

  • 你能展示一些示例输入数据和预期输出吗?
  • 示例:text here @user1 test @user2 提取 @user1, @user2 并在找到 foreach user 的情况下插入数据库
  • @OtávioBarreto 刚刚尝试了一个简单的解决方案来获得您想要的结果

标签: php regex function for-loop foreach


【解决方案1】:

您可以使用如下函数来根据在给定字符串中找到的用户生成和执行查询。它使用preg_match_all 查找字符串中命名的所有用户。

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\'')

Demo on 3v4l.org

更新

根据问题中的更新代码,我认为这应该可以工作,尽管无法访问数据库,我无法进行测试。

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

【讨论】:

  • 我正在使用 PDO,应该也可以使用 pdo 对吗?我只需要将 pdo var 传递给函数。
  • @OtávioBarreto 是的,只需将 pdo var 传递给函数,然后使用类似 $result = $pdo-&gt;query($query);
  • 我用我需要的 PDO bindParam 更新了这个问题,我还在想办法。
  • @OtávioBarre查看我的编辑。我认为它应该非常接近您的需求。
  • @OtávioBarreto 不用担心。我很高兴能帮上忙。
【解决方案2】:

您可以通过在 php.ini 中创建一个简单的函数来获得所需的结果。另外,这里我在字符串中添加了一个额外的空格来获取最后一个子字符串

<?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] => 用户 1 [1] => 用户2)

【讨论】:

    猜你喜欢
    • 2021-02-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-21
    • 1970-01-01
    • 2023-03-17
    • 2019-10-23
    • 2014-01-19
    相关资源
    最近更新 更多