DuaneCawaling Posted March 6, 2013 Share Posted March 6, 2013 $str= "WAL bal"; $message = explode(" ",$word); if(preg_match('/(register|bal|menu|snacks|drinks|buy|pass)/', $str)){ if($message[1]){ echo "KEYWORD--OK <br>"; return $str; }else echo "Invalid keyword, try again."; this works okay, the only problem is that if $str = WAL balance, it is still strue coz it has "bal" and i dont want that to happen.. what can i do to limit the $message[1] as such register|bal|menu|snacks|drinks|buy|pass ONLY?? Quote Link to comment https://forums.phpfreaks.com/topic/275297-string-check/ Share on other sites More sharing options...
requinix Posted March 6, 2013 Share Posted March 6, 2013 Why use regular expressions at all? in_array does what you (apparently) need. Quote Link to comment https://forums.phpfreaks.com/topic/275297-string-check/#findComment-1416861 Share on other sites More sharing options...
DuaneCawaling Posted March 6, 2013 Author Share Posted March 6, 2013 the $str is a string. in_array() needs an array as a parameter. to make the string an array.str_split is used. a problem occurs because the string's parameters 'register|bal|menu|snacks|drinks|buy|pass' are not equal in length.str_split arrays with equal length Quote Link to comment https://forums.phpfreaks.com/topic/275297-string-check/#findComment-1416872 Share on other sites More sharing options...
requinix Posted March 6, 2013 Share Posted March 6, 2013 (edited) Right. But you could just put them in an array instead of a string: $words = array("register", "bal", "menu", "etc");[edit] And even if you didn't want to do that, you would have to use explode() (which you've shown you know about) not str_split(). Edited March 6, 2013 by requinix Quote Link to comment https://forums.phpfreaks.com/topic/275297-string-check/#findComment-1416875 Share on other sites More sharing options...
DuaneCawaling Posted March 6, 2013 Author Share Posted March 6, 2013 i did not think of that..sorry for that..anyways thank you for the advice it works perfectly fine now Quote Link to comment https://forums.phpfreaks.com/topic/275297-string-check/#findComment-1416893 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.