Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Php find String in foreach

How can I find an highlight a string in a foreach?

This loops through all users of my database and I want to find all lets say $um.

$um = "Um";
foreach($users as $user){
     # find here the string um or something
     echo $user;
}

How do I accomplish this?

like image 852
Slark Avatar asked Nov 27 '25 15:11

Slark


1 Answers

In reality, I would do this in the query.. ..but

$um = "Um";

foreach($users as $user){

    if(strpos($user,$um) !== false){

        echo $user;

    }
}

strpos() returns a string position, so a return value of '0' would be a match at the beginning of the string. So, check for 'not false' http://php.net/manual/en/function.strpos.php

like image 51
MaggsWeb Avatar answered Nov 30 '25 05:11

MaggsWeb



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!