Zugzwangle Posted July 15, 2012 Share Posted July 15, 2012 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? Quote Link to comment Share on other sites More sharing options...
.josh Posted July 15, 2012 Share Posted July 15, 2012 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; } } } Quote Link to comment Share on other sites More sharing options...
Zugzwangle Posted July 15, 2012 Author Share Posted July 15, 2012 Thanks Josh.. that is a nice and easy solution. Quote Link to comment Share on other sites More sharing options...
.josh Posted July 15, 2012 Share Posted July 15, 2012 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. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.