PDA

View Full Version : substring compare


kenny873
05-14-2003, 09:53 AM
if I want to compare two strings...for example

$compare_1 = "http://www.codingforums.com/error.php?abc=123";

$compare_2 = "http://www.codingforums.com/index.php";

$compare = "error.php";


how do I get sub string from $compare_1 or $compare_2 to see if the URL has "error.php" (i.e. $compare)

if substr(($compare_1 has error.php) == $compare)
echo "matched";

how can I find if $compare_1 or $compare_2 consist of "error.php"

Weirdan
05-14-2003, 10:55 AM
strpos (http://www.php.net/manual/en/function.strpos.php) is what you need.

firepages
05-14-2003, 01:57 PM
strstr() may be better here for a quick check ?


<?
if(strstr($compare_1,$compare)){
echo 'match found';
}
?>

Weirdan
05-14-2003, 02:35 PM
2firepages

from php.net
Note: If you only want to determine if a particular needle occurs within haystack , use the faster and less memory intensive function strpos() instead.

firepages
05-14-2003, 03:29 PM
fair enough , my CHM version manual which I use mostly does not have that note so cheers ! , stristr() must be even less efficient as it is case sensitive but no note to mention !

Spookster
05-14-2003, 05:35 PM
Originally posted by Weirdan
2firepages

I do not see that anywhere in the manual either.

In any case that function returns only the starting position of the substring.

As firepages stated even if strstr is slower and uses more memory it would be because it is doing case sensitive checking. There is the other version that is case insenstive:

http://www.php.net/manual/en/function.stristr.php

And really though we are only talking about probably a 1000th of a millisecond difference in speed in the case of such a short string.

Weirdan
05-14-2003, 06:18 PM
Originally posted by Spookster
And really though we are only talking about probably a 1000th of a millisecond difference in speed in the case of such a short string.
Yeah, I know. But firepages have said "better"... ;)

Spookster
05-14-2003, 08:16 PM
Originally posted by Weirdan
Yeah, I know. But firepages have said "better"... ;)

Better does not necessarily mean more efficient, less memory intensive, etc. :p

With firepages the term better could have a yakfulls of different meanings. He's a yakky kinda guy :D

kenny873
05-15-2003, 02:36 AM
I have used the following statements:

$compare = "error";
if (ereg($compare,$compare_1)){
echo "Matched";
} else {
echo "Not Matched";
}

Spookster
05-15-2003, 02:52 AM
Originally posted by kenny873
I have used the following statements:

$compare = "error";
if (ereg($compare,$compare_1)){
echo "Matched";
} else {
echo "Not Matched";
}

How dare you go against all of our suggestions!!!!! Grrrr. :D

firepages
05-15-2003, 03:35 AM
lol and after we have been on about efficiency ... ;)

ereg() is the very slowest method you could choose i.e. never use regex if you can do it another way , if you have to use regex then the PERL compatible functions are supposedly faster than ereg etc.

but as a diplomatic reply it cant be beaten :D

Spooks - you calling ME yakky ! , pots , kettles ~