在列值上使用 preg_match_all 内部循环时出现问题

Issue while using preg_match_all inside loop over a column value

我想对循环内的“msg”列值使用 preg_match_all()

但在我的循环中,它不适用于所有“msg”列值,仅适用于最后一个。

以下是我写的代码:

$query = "SELECT email_id, msg FROM emailtry";
$result = mysqli_query($link, $query);
while ($obj = mysqli_fetch_object($result)){
    printf("%s (%s) \n\n\n",$obj->email_id, $obj->msg);
    $string = $obj->msg;
    
    if (is_array($result) || is_object($result) ){  
        foreach($result as $result){
            if (preg_match_all("/Dynamic Screen:\s+[A-Za-z0-9_-]+\s+[A-Za-z0-9_-]+/", $string, $notificationtype)){
                var_dump($notificationtype);
            }else{
                return false;
            }
            if (preg_match_all("/[A-Za-z0-9_-]+@[A-Za-z0-9_-]+\.[A-Za-z0-9_-]+/", $string, $bodylogonused)){
                var_dump($bodylogonused);
            }else{
                return false;
            }
        }
    }
}

while() 已经在执行时,它里面的 foreach() 需要什么?

按如下操作:

$query = "SELECT email_id, msg FROM emailtry";
$result = mysqli_query($link, $query);
while ($obj = mysqli_fetch_object($result)){
    if (preg_match_all("/Dynamic Screen:\s+[A-Za-z0-9_-]+\s+[A-Za-z0-9_-]+/", $obj->msg, $notificationtype)){
        var_dump($notificationtype);
    }
    if (preg_match_all("/[A-Za-z0-9_-]+@[A-Za-z0-9_-]+\.[A-Za-z0-9_-]+/", $obj->msg, $bodylogonused)){
        var_dump($bodylogonused);
    }
}

注意: 正如@mickmackusa 建议的那样,在代码中使用 [\w-] 而不是 [A-Za-z0-9_-]。 (更简洁)

$query = "SELECT email_id, msg FROM emailtry";
$result = mysqli_query($link, $query);
while ($obj = mysqli_fetch_object($result)){
    if (preg_match_all("/Dynamic Screen:\s+[\w-]+\s+[\w-]+/", $obj->msg, $notificationtype)){
        var_dump($notificationtype);
    }
    if (preg_match_all("/[\w-]+@[\w-]+\.[\w-]+/", $obj->msg, $bodylogonused)){
        var_dump($bodylogonused);
    }
}