php question
Heya ppls, got a php question for ya(im refreshing my kung f00)
In regards to string comparisons, finding say for example
string "Monday" in string "Monday the 13th of June was a great day"
are there any functions that can return a true/false from the string comparison?
strstr and stristr dont seem to be able to produce a True return, only a False. I'm getting a return at the moment using strpos to determine my if statements logic path, it works, but is fugly. Anything more elegant or practical would b 'preciated.
Cheers
"I ’ll make thee glorious by my pen, And famous by my sword." - James Graham, Marquess of Montrose (1612–1650)
Abhishek Reddy posted this at 04:10 — 31st October 2002.
He has: 3,348 posts
Joined: Jul 2001
strstr and stristr don't return true? That's odd. Can you post code? It could be down to some silly error.
Mark Hensler posted this at 04:16 — 31st October 2002.
He has: 4,048 posts
Joined: Aug 2000
function prototype:
string strstr ( string haystack, string needle)
"Returns part of haystack string from the first occurrence of needle to the end of haystack."
for a boolean return, try this:
$in_string = is_string(strstr($haystack, $needle));
Mark Hensler
If there is no answer on Google, then there is no question.
hagar posted this at 04:35 — 31st October 2002.
They have: 104 posts
Joined: Oct 2002
Abi: r u sure they are sposed to return true? im using php-4.2.3-Win32, is it different from what you are using? php manual says u get false or parts of the haystack.
Mark:
yeah i've tried the is_string method, for some reason it can go real screwy on a blank needle (dont ask me why:D)
I'm using file to turn a URL into an array(my haystack), reading each line of the array to check for an occurance of needle, if found, it does some other manipulations on the string from the array, pretty easy stuff really and it currently works, was just wondering if there was a specific function that returned two logical results, instead of 1, and a bit:D using is_string or strpos just doesnt seem "right" to me, seeing php seems to have such a vast code base, lacking this one function seems "weird"
$pos = strpos(strtolower($arts), strtolower($strW));
if ($pos === false) {
this is what im using currently, works, and also helps a bit because I use $pos in the next line of code for further string manipulation, so it kills two birds with one stone I spose. thanks fellas;)
"I ’ll make thee glorious by my pen, And famous by my sword." - James Graham, Marquess of Montrose (1612–1650)
hagar posted this at 05:20 — 31st October 2002.
They have: 104 posts
Joined: Oct 2002
hmmm ok wtf? lol looks like strstr CAN produce a boolean result... the manual is maybe wrong??
<?
if (strstr($_SERVER["HTTP_USER_AGENT"], "MSIE")) {
echo "You are using Internet Explorer";
}
?>
try it, works fine, I hate bad docco! lol
"I ’ll make thee glorious by my pen, And famous by my sword." - James Graham, Marquess of Montrose (1612–1650)
Mark Hensler posted this at 06:30 — 31st October 2002.
He has: 4,048 posts
Joined: Aug 2000
PHP assumes anything that is not NULL/0/FALSE to be TRUE (when compared as a boolean value). So, if strstr() returns a string, it will be evaluated as TRUE in an IF statement.
I know what you mean about the feeling wrong. Other high level languages have simple boolean functions like in_string(). You could always write a function to do this...
Mark Hensler
If there is no answer on Google, then there is no question.
Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.