Help | Site Map
Connecting Tech Pros Worldwide
 
 
LinkBack Thread Tools
  #1  
Old March 7th, 2006, 05:05 PM
Jim Carlock
Guest
 
Posts: n/a
Default 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.


  #2  
Old March 7th, 2006, 10:45 PM
Janwillem Borleffs
Guest
 
Posts: n/a
Default 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


  #3  
Old March 8th, 2006, 05:25 AM
Richard Levasseur
Guest
 
Posts: n/a
Default 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.

  #4  
Old March 9th, 2006, 08:05 PM
Jim Carlock
Guest
 
Posts: n/a
Default 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.


 

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over network members.
Post your question now . . .
It's fast and it's free

Popular Articles