Jump to content

Searching for usernames.


Zugzwangle

Recommended Posts

I have a php array called $username_arr, of usernames. Some contain spaces.

I would like to search another array, called $event_name, for the usernames contained in $username_arr.

 

For example, "at 20.00, Steve Dobson went to the park".  If "Steve Dobson" in the array $username_arr, I want to know!!!

 

This is what I have got so far, although I have taken it out of the for loop as you can see.

if (preg_match('/^['.$username_arr[$i].']$/', $event_name[$j]))
{
// execute command
}

 

How should I go about doing this?

Link to comment
https://forums.phpfreaks.com/topic/265702-searching-for-usernames/
Share on other sites

There are a ton of things wrong w/ your regex but I'm not gonna get into that because you don't really need regex for this.

 

foreach ($event_name as $ev) {
  foreach ($username_arr as $u) {
    if ( stripos($ev, $u) !== false ) {
      // array of found users
      $found_users[] = $u;
    }
  }
}

 

there is one small caveat about this though... it will match on partial matches.  For example if you have "some string with John Smithsonian" and you have the name "John Smith" it will match.  If that is a problem for you then will have to go back to regex.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.