Jump to content

Help with contains strstr()


mcmuney

Recommended Posts

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!

Link to comment
https://forums.phpfreaks.com/topic/267162-help-with-contains-strstr/
Share on other sites

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 ==.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.