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); ? 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. 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 ? 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()? 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? 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)? 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! Link to comment https://forums.phpfreaks.com/topic/118658-solved-simple-regex/#findComment-611048 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.