PHP and comparing strings

MarthaJ Sayers
MarthaJ Sayers used Ask the Experts™
on
I have two strings that I am comparing. My coding is working but I do not think the way I coded was sturdy.
Perhaps it is sturdy enough - I just need to know if the string. $subject, (which is extracted from an email), contains the values.
I compare after doing some decoding i.e. imap_base64 etc to the message.
I know that I can use strcasecmp but how do I retrieve the results ??
below is what I am currently using -
$subject = 'Notification of Meeting';
$strChrForFwd = 'Fwd: Notification of Meeting';
if ((substr_count($subject, $strAch) > 0) ||  (substr_count($subject, $strChrForFwd) > 0))

Open in new window


Thank you...
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Commented:
Well, your example wouldn't run because you are searching for a value that begins with "Fwd:" but your subject doesn't have that.

The fastest search is simply strpos, but it is case-sensitive. The slightly-slower case-insensitive version is stripos().

if(stripos($subject, $searchFor) !== false)
{
   ...runs when $searchFor is found inside $subject...
}

Author

Commented:
I made an error when I copied and pasted it to EE. MY apologies.
This is the actual coding
$strAch = ' Notification of Meeting';
$subject = 'Notification of Meeting';
$strChrForFwd = 'Fwd: Notification of Meeting';
if ((substr_count($subject, $strAch) > 0) ||  (substr_count($subject, $strChrForFwd) > 0))

Open in new window

Commented:
Okay. It's still the same issue as I described before, though. The $strAch has a space at the beginning which is not in $subject.
Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

Author

Commented:
Thank you -  I feel pretty silly !!! I need new glasses maybe?? LOL.
But is substr_count the best to use in comparing strings ??

Commented:
No, you should use strpos or stripos for what you're doing. The substr_count function counts how many times X appears in Y, so it has to look at the entirety of Y to get that count, even if X is immediately found at the beginning of Y.

In contrast, both strpos and stripos only look for the first occurrence of X in Y. So if Y is a really really long string and X is found near the beginning of Y, then it doesn't have to check the rest of Y.

Granted, you're dealing with email subjects which are very tiny, so chances are that the performance differences will be so tiny that it wouldn't matter which approach you used. However, I would still recommend using strpos or stripos just to get into the good habit of using them. Those should be your default choice for searching for strings inside strings.

Author

Commented:
Thank you for responding.
So you are saying instead of substr_count use strpos or stripos and still check for > 0 ... do I understand correctly ??
When I read and read you comment over, it makes much sense by the way you have explained it.
 and I see/understand why you what you are saying it..

Commented:
Actually you want to check to make sure the result is not false.

 if(stripos($subject, $searchFor) !== false)
{
   ...runs when $searchFor is found inside $subject...
}   

Open in new window

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial