0

I want to use preg_match_all() for "msg" column values inside loop.

But in my loop it won't work with all "msg" column values, working only with last one.

Following is the code I have written:

$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;
            }
        }
    }
}
3
  • 1
    You shouldn't do this: foreach($result as $result). Try giving them different names. I don't know if that will help your issue or not but maybe.
    – Joel M
    Mar 2, 2021 at 7:16
  • When already while is executing then what's the need of foreach() inside it? Mar 2, 2021 at 7:24
  • I have voted this question to be Unclear. Your actual output versus your desired output is not clearly expressed. Are you saying that $string is only retaining the last row's msg -- that would make sense. If you are only trying to access $string then your minimal reproducible example doesn't need to show any of the if (is_array(... ) condition block. Why do you have a loop breaking return? We don't see where/why this would be a good idea for the code provided. If you are searching for email addresses with the second pattern, it will not catch all email addresses. Mar 23, 2021 at 22:48

1 Answer 1

1

When already while() is executing then what's the need of foreach() inside it?

Do like below:

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

Note: As @mickmackusa suggested, use [\w-] instead of [A-Za-z0-9_-] in your code. (more concise)

$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);
    }
}
3
  • @mickmackusa I am actually not good in regex, what I did is just helped OP to rectify his code to make it working. In case you have any you can post answer. I will appreciate that Mar 24, 2021 at 5:42
  • @mickmackusa thanks for sharing code suggestion, added that, could you please have a look that everything fine. I am really bad in regex....Thanks Mar 24, 2021 at 7:28
  • Well, as I commented under the question, there are valid email addresses that eill not be captured. Mar 24, 2021 at 8:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.