468,247 Members | 1,331 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,247 developers. It's quick & easy.

Better Ways Compare Strings: Exact Matches

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.
Mar 7 '06 #1
3 2121
Jim Carlock wrote:
(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.


You might find preg_grep useful:

http://www.php.net/preg_grep
JW
Mar 7 '06 #2
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.

Mar 8 '06 #3
"Richard Levasseur" <ri********@gmail.com> wrote:
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.


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.
Mar 9 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

39 posts views Thread by bazad | last post: by
14 posts views Thread by Samuel R. Neff | last post: by
11 posts views Thread by Magnus Lycka | last post: by
8 posts views Thread by Dilip | last post: by
1 post views Thread by mathieu | last post: by
reply views Thread by NPC403 | last post: by
reply views Thread by zattat | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.