using STRIPOS()

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
tcloud
Forum Newbie
Posts: 6
Joined: Fri May 07, 2010 6:11 am

using STRIPOS()

Post by tcloud »

I need to know if a string exists within a string and am using STRIPOS() for that purpose.

Problem -- when the needle string is the first thing in the string, STRIPOS() returns zero or false.

I don't care what function I use, but I need to get a "true" when the needle string is present anywhere in the haystack string, even at location zero.

Is there some way to set up the STRIPOS() statement?

I've tried:

Code: Select all

if ((stripos($title, 'needle') > 0 )
if ((stripos($title, 'needle') = true )
and even

Code: Select all

if ((stripos($title, 'needle') >= 0 )
which returns true for everything, even when the needle string is NOT present.

Any help appreciated -- including a different approach.

thanks,
Tom
User avatar
DigitalMind
Forum Contributor
Posts: 152
Joined: Mon Sep 27, 2010 2:27 am
Location: Ukraine, Kharkov

Re: using STRIPOS()

Post by DigitalMind »

Code: Select all

if (stripos($title, 'needle') !== false)
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Re: using STRIPOS()

Post by s.dot »

Yes, use the !== comparison operator.
This checks type as well.

0 == false //this is true
0 === false //this is not true

http://php.net/manual/en/language.opera ... arison.php
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
Post Reply