| if string contains .
|
webfoo

msg:4243345 | 9:58 pm on Dec 16, 2010 (gmt 0) | 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?
|
deejay

msg:4243352 | 10:15 pm on Dec 16, 2010 (gmt 0) | try !==
|
deejay

msg:4243353 | 10:17 pm on Dec 16, 2010 (gmt 0) | 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"; } ?>
|
Matthew1980

msg:4243360 | 10:28 pm on Dec 16, 2010 (gmt 0) | 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
|
webfoo

msg:4243363 | 10:42 pm on Dec 16, 2010 (gmt 0) | 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.
|
Matthew1980

msg:4243365 | 10:51 pm on Dec 16, 2010 (gmt 0) | ^^^ 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
|
Nutter

msg:4243413 | 1:27 am on Dec 17, 2010 (gmt 0) | 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.
|
webfoo

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

msg:4243660 | 4:00 pm on Dec 17, 2010 (gmt 0) | 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
|
|
|