Page 1 of 1
strpos - why won't it find the word?
Posted: Wed Nov 06, 2013 8:34 am
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.
Re: strpos - why won't it find the word?
Posted: Wed Nov 06, 2013 8:48 am
by simonmlewis
Bingo:
Code: Select all
if (strpos($catname, 'shirt') === false) {
echo "yes";
}
Re: strpos - why won't it find the word?
Posted: Wed Nov 06, 2013 8:51 am
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.
Re: strpos - why won't it find the word?
Posted: Wed Nov 06, 2013 9:05 am
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.
Re: strpos - why won't it find the word?
Posted: Wed Nov 06, 2013 11:59 am
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
Re: strpos - why won't it find the word?
Posted: Wed Nov 06, 2013 12:14 pm
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.
Re: strpos - why won't it find the word?
Posted: Wed Nov 06, 2013 12:20 pm
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";
}
Re: strpos - why won't it find the word?
Posted: Wed Nov 06, 2013 1:31 pm
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';.
Re: strpos - why won't it find the word?
Posted: Wed Nov 06, 2013 4:23 pm
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...