deadonarrival Posted August 7, 2008 Share Posted August 7, 2008 I just need the regex to find a whole word within a string. Eg $string = "then he said hello to me"; $word = "hello"; if(_regex here_) { //$word is in $string } else { //$word is not in string } I'm completely clueless with regex. Is it just something like preg_match("^(.*)$word(.*)$",$string); ? Quote Link to comment https://forums.phpfreaks.com/topic/118658-solved-simple-regex/ Share on other sites More sharing options...
effigy Posted August 7, 2008 Share Posted August 7, 2008 If you're only working with ASCII, use /\bword\b/. Otherwise, see this post. Quote Link to comment https://forums.phpfreaks.com/topic/118658-solved-simple-regex/#findComment-610915 Share on other sites More sharing options...
deadonarrival Posted August 7, 2008 Author Share Posted August 7, 2008 I need it to find the word, but be case-insensitive and find the word within other words aswell. Is it just a case of using substr() on the second argument and using /\bword\b/i ? Quote Link to comment https://forums.phpfreaks.com/topic/118658-solved-simple-regex/#findComment-610989 Share on other sites More sharing options...
effigy Posted August 7, 2008 Share Posted August 7, 2008 How about stristr()? Quote Link to comment https://forums.phpfreaks.com/topic/118658-solved-simple-regex/#findComment-611013 Share on other sites More sharing options...
deadonarrival Posted August 7, 2008 Author Share Posted August 7, 2008 Hmm, never seen that before - nice suggestion. But if I need to use it as if(function(regex,$str)){} will it still return true? Since it returns the remainder of the string? Quote Link to comment https://forums.phpfreaks.com/topic/118658-solved-simple-regex/#findComment-611029 Share on other sites More sharing options...
effigy Posted August 7, 2008 Share Posted August 7, 2008 Example #2 for stristr() demonstrates this. Do you need a regex (pattern)? Quote Link to comment https://forums.phpfreaks.com/topic/118658-solved-simple-regex/#findComment-611033 Share on other sites More sharing options...
deadonarrival Posted August 7, 2008 Author Share Posted August 7, 2008 Whoops, looked at the w3schools (first google result for stristr) instead of php.net I think that's perfect, thankyou! Quote Link to comment https://forums.phpfreaks.com/topic/118658-solved-simple-regex/#findComment-611048 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.