mcmuney Posted August 16, 2012 Share Posted August 16, 2012 I'm trying to use a contain function to determine if a string contains the @ symbol; however, it doesn't seem to be working correctly. $find = strstr($comment, '@'); I'm noticing that the @ symbol is being determined in the following cases: @ abc@ xyz@ However, if the @ is followed by a space or other values, it's not being detected by $find, for example: @ abc abc@ abc abc @ abc I want @ to be detected no matter where it is. Help! Quote Link to comment https://forums.phpfreaks.com/topic/267162-help-with-contains-strstr/ Share on other sites More sharing options...
mcmuney Posted August 16, 2012 Author Share Posted August 16, 2012 I've also tried this: (strpos($comment,'@') == false){ //action But this only false when the string does not begin with @. I need it to be false if the @ exist ANYWHERE. Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/267162-help-with-contains-strstr/#findComment-1369812 Share on other sites More sharing options...
Barand Posted August 16, 2012 Share Posted August 16, 2012 as 0 == false you need to use if (strpos($comment,'@') === false){ Quote Link to comment https://forums.phpfreaks.com/topic/267162-help-with-contains-strstr/#findComment-1369832 Share on other sites More sharing options...
ManiacDan Posted August 16, 2012 Share Posted August 16, 2012 Barand is correct about strpos, but strstr works differently. Strstr returns the matched string. All three of these examples worked for me: php > $a = '123@456'; php > $b = '123@ 456'; php > $c = '123 @ 456'; php > var_dump(strstr($a, '@') == true); bool(true) php > var_dump(strstr($b, '@') == true); bool(true) php > var_dump(strstr($c, '@') == true); bool(true) php > var_dump(strstr($a, '@')); string(4) "@456" php > var_dump(strstr($b, '@')); string(5) "@ 456" php > var_dump(strstr($c, '@')); string(5) "@ 456" I'm not sure why you think it wasn't matching with the space after it for strstr. Either way, strpos !== false is what you want. Make sure you realize that !== is correct, and != is incorrect. === and its counterpart !== are required here, not ==. Quote Link to comment https://forums.phpfreaks.com/topic/267162-help-with-contains-strstr/#findComment-1369900 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.