Jump to content

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.