strpos - why won't it find the word?

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
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

strpos - why won't it find the word?

Post by simonmlewis »

Code: Select all

$a = 'how are you';
if (strpos($a,'are')) {
    echo 'true';
}

I am using this as an example of what I am trying it do - which is pretty basic!
$a has dynamic content going into it. I want to check if that dynamic content contains a word.

So $a could "black t-shirt".
I want to check is "shirt" is in that string.

Therefore, am trying this but it's coming back with nothing:

Code: Select all

if (strpos($a,'shirt')) {
    echo 'true';
}

Bear in mind $a DOES have content it in, as I can echo it.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: strpos - why won't it find the word?

Post by simonmlewis »

Bingo:

Code: Select all

if (strpos($catname, 'shirt') === false) {
echo "yes";
}
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: strpos - why won't it find the word?

Post by simonmlewis »

Damn it no that doesn't work.
If I post thru with that term in the variable, it doesn't bring it up, if I don't pass that through, it does.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: strpos - why won't it find the word?

Post by simonmlewis »

Code: Select all

// The "i" after the pattern delimiter indicates a case-insensitive search
if (preg_match("/shirt/i", "$catname")) {
    echo "A match was found.";
} else {
    echo "A match was not found.";
}
This works perfectly.
Duno why strpos didn't work tho.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: strpos - why won't it find the word?

Post by AbraCadaver »

Nice dialogue you have going with yourself, so I hate to interrupt, but:

Code: Select all

$catname = 'black t-shirts';

if (strpos($catname, 'shirt') !== false) {
    echo "yes";
}
echos yes
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: strpos - why won't it find the word?

Post by simonmlewis »

Yes, but not if you don't put n the page
$catname = 'black t-shirts';

IF you just take what's in the variable without writing it as you put it here, it doesn't work.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: strpos - why won't it find the word?

Post by AbraCadaver »

simonmlewis wrote:Yes, but not if you don't put n the page
$catname = 'black t-shirts';

IF you just take what's in the variable without writing it as you put it here, it doesn't work.
Sure it does. If shirt is part of $catname. Post the output of:

Code: Select all

echo $catname;

if (strpos($catname, 'shirt') !== false) {
    echo "yes";
}
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: strpos - why won't it find the word?

Post by simonmlewis »

If you say so.
But I was running that exact script inside a script that would only run if $catname had something in it. In this case, it had the appropriate terms.
Yet that script ran nothing, unless I said $catname = 'shirt';.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: strpos - why won't it find the word?

Post by requinix »

Then you had your logic backwards somewhere:

Code: Select all

$a='shirt';
if (strpos($a,'shirt')) {
    echo 'true';
}
will definitely not output "true". You say it did which means you had something backwards. It would also explain why you didn't get any output when it should have, and makes me wonder whether you got output when you shouldn't have...
Post Reply