robert_gsfame Posted December 13, 2010 Share Posted December 13, 2010 I have this $val="my friend is a female"; how to check whether a word "male" is there or not using preg_match() i wish to get FALSE result for that case and TRUE if i modify a bit into $val="both male/female" thx in advance! Quote Link to comment https://forums.phpfreaks.com/topic/221485-preg_match-question/ Share on other sites More sharing options...
laffin Posted December 13, 2010 Share Posted December 13, 2010 not shure wut u mean. but here is a function, checks for female/male <?php $val="male"; $gender=preg_match('/\b(?:fe)?male/',$val,$match)?(substr($match[0],0,1)=='m'?1:2):FALSE; echo $gender .PHP_EOL; echo ($gender==1?'Male'$gender==2?'Female':'Other')) . PHP_EOL; ?> Quote Link to comment https://forums.phpfreaks.com/topic/221485-preg_match-question/#findComment-1146536 Share on other sites More sharing options...
robert_gsfame Posted December 13, 2010 Author Share Posted December 13, 2010 i want to use preg_match() to check whether $val consists of a word "male" or not and i wish this way $val="my friend is a female" ---> FALSE $val="both male/female" -----> TRUE Quote Link to comment https://forums.phpfreaks.com/topic/221485-preg_match-question/#findComment-1146541 Share on other sites More sharing options...
laffin Posted December 13, 2010 Share Posted December 13, 2010 break the routine apart, it can be modified for those needs. the hard part the regex pattern is already working. if u know that preg_match returns 0 or 1 (0 no match, 1 match) than its not hard to figure out the rest. Quote Link to comment https://forums.phpfreaks.com/topic/221485-preg_match-question/#findComment-1146544 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.