Connecting Tech Pros Worldwide Forums | Help | Site Map

Better Ways Compare Strings: Exact Matches

Jim Carlock
Guest
 
Posts: n/a
#1: Mar 7 '06
I'm creating a lot of arrays. Some of the arrays carry words
that get duplicated...

For example:
$aCities = {"Durham", "Raleigh", "Raleigh-Durham", "Salem", "Winston", "Winston-Salem" }

I've been using stristr alot but that ends up meaning that I
must take the arrays out of alphabetical order and in the
end it complicates things on my part. I'll comparing it to the
strcmp() function right at the moment but I ran into some
difficulties with strcmp() a couple nights ago, along the lines
of having to convert the strings in the array lower-case (all
my arrays are currently stored in a ucwords() format). The
dashes in the names also present a bit of a problem.

The way I'm currently doing the comparisons...

function GetCity($sSearchThis) {
$aAllCities = GetAllCitiesArray();
// make sure parameter is lowercase
$sSearchMe = strtolower($sSearchThis);
foreach ($aAllCities as $sThisCity) {
if (stristr($sSearchMe, $sThisCity) !== FALSE) {
return($sThisCity);
}
// default to raleigh
return("raleigh");
}

What's the best way to accomplish this or is there a better
way under the following conditions:

(1) The item passed in IS a string (not an array).
(2) A default string is always returned when no match is found.
(3) The alphabetical order of the array must remain alphabetized.
(4) The dashes are not required.

Thanks for taking your time and thanks for your consideration in
helping.

Jim Carlock
Post replies to the newsgroup.



Janwillem Borleffs
Guest
 
Posts: n/a
#2: Mar 7 '06

re: Better Ways Compare Strings: Exact Matches


Jim Carlock wrote:[color=blue]
> (1) The item passed in IS a string (not an array).
> (2) A default string is always returned when no match is found.
> (3) The alphabetical order of the array must remain alphabetized.
> (4) The dashes are not required.
>[/color]

You might find preg_grep useful:

http://www.php.net/preg_grep


JW


Richard Levasseur
Guest
 
Posts: n/a
#3: Mar 8 '06

re: Better Ways Compare Strings: Exact Matches


How about case insensitive string comparison? strcasecmp()
http://us3.php.net/manual/en/function.strcasecmp.php

I also suggest array_search, in_array.

Those are case sensitive, though, but it may be easier to store them
all in lowercase and then ucwords() them before they're
returned/displayed, or use array_walk or array_map to apply
ucwords/strtolower to the source array.

Jim Carlock
Guest
 
Posts: n/a
#4: Mar 9 '06

re: Better Ways Compare Strings: Exact Matches


"Richard Levasseur" <richardlev@gmail.com> wrote:[color=blue]
> How about case insensitive string comparison? strcasecmp()
> http://us3.php.net/manual/en/function.strcasecmp.php
>
> I also suggest array_search, in_array.
>
> Those are case sensitive, though, but it may be easier to store
> them all in lowercase and then ucwords() them before they're
> returned/displayed, or use array_walk or array_map to apply
> ucwords/strtolower to the source array.[/color]

Thanks Richard. I found strcmp() while searching and I knew of
strstr() versus stristr(), and so searched for stricmp().

Thanks much.

Jim Carlock
Post replies to the group.


Closed Thread


Similar PHP bytes