Welcome to WebmasterWorld Guest from 54.197.171.28

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

if string contains .

   
9:58 pm on Dec 16, 2010 (gmt 0)

5+ Year Member



Hello all, it's been a while, but I am back cracking at some code.

What I need it to do is this: If $string contains a certain phrase, do one thing. If $string does not contain that phrase, do something else.

This is the code that is not working:
if(strpos($string,"phrase")!="false") { 
// DO SOMETHING;
}
else {
// DO SOMETHING ELSE;
}

I am sure its something stupid. What is the problem?
10:15 pm on Dec 16, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



try !==
10:17 pm on Dec 16, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



ppfft. stupid hitting submit too early.

Also found the following example in the php manual re stripos - the difference I believe is that stripos is case insensitive, which might be relevant for you.

<?php
$findme = 'a';
$mystring1 = 'xyz';
$mystring2 = 'ABC';

$pos1 = stripos($mystring1, $findme);
$pos2 = stripos($mystring2, $findme);

// Nope, 'a' is certainly not in 'xyz'
if ($pos1 === false) {
echo "The string '$findme' was not found in the string '$mystring1'";
}

// Note our use of ===. Simply == would not work as expected
// because the position of 'a' is the 0th (first) character.
if ($pos2 !== false) {
echo "We found '$findme' in '$mystring2' at position $pos2";
}
?>
10:28 pm on Dec 16, 2010 (gmt 0)

WebmasterWorld Senior Member 5+ Year Member



Hi all,

Surely it would be better doing a substr() rather than strpos() or even using preg_match() (with the 'i' flag set for insensitivity) would do the trick.

Carefull with the use of === as this is only used for checking the same types against each other, other than that, your logic is right.

Cheers,
MRb
10:42 pm on Dec 16, 2010 (gmt 0)

5+ Year Member



Does it matter if the phrase contains parentheses? I probably should have mentioned the phrase we are trying to detect is (E). At this point it, I believe it is url-decoded. It will always be an upper case E inside parentheses.
10:51 pm on Dec 16, 2010 (gmt 0)

WebmasterWorld Senior Member 5+ Year Member



^^^

Don't think it matters, at this point you are passing a string into a function, and using that function to parse that string to look for a pattern that you have specified; having said that, if your using preg_ functions, you would need to account for that I think, but unfortunately regex isn't really my forte.

Don't forget that you can force the given string to be lower case by using strtolower(), that way everything is on a level field.

Cheers,
MRb
1:27 am on Dec 17, 2010 (gmt 0)

10+ Year Member



You have !="false" with quotes around false.

And it probably should be === false instead of == false. You need to make sure it's not returning 0 for a find in the first position. For PHP 0==false, but 0!==false because the data types are different.
2:37 pm on Dec 17, 2010 (gmt 0)

5+ Year Member



Thanks guys, finally got it working. For the reccord, the final code was:
if(strchr($string,"(E)")!==FALSE) {
// DO SOMETHING;
}
else {
DO SOMETHING ELSE;
}
4:00 pm on Dec 17, 2010 (gmt 0)

WebmasterWorld Senior Member 5+ Year Member



Hi all,

>>You have !="false" with quotes around false.

Well spotted there Nutter, I hadn't noticed that, but yes, quoting the FALSE meant as it was trying to equate to a string and not the Boolean/true/false as was needed.

Good that you have it working, you could probably make that more inverted, but for now, I can't think straight enough to show an example ;)

Happy coding!

Cheers,
MRb