Better Ways Compare Strings: Exact Matches

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Jim Carlock

    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($sSearc hThis) {
    $aAllCities = GetAllCitiesArr ay();
    // make sure parameter is lowercase
    $sSearchMe = strtolower($sSe archThis);
    foreach ($aAllCities as $sThisCity) {
    if (stristr($sSear chMe, $sThisCity) !== FALSE) {
    return($sThisCi ty);
    }
    // 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

    #2
    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:

    Return array entries that match the pattern



    JW


    Comment

    • Richard Levasseur

      #3
      Re: Better Ways Compare Strings: Exact Matches

      How about case insensitive string comparison? strcasecmp()


      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.

      Comment

      • Jim Carlock

        #4
        Re: Better Ways Compare Strings: Exact Matches

        "Richard Levasseur" <richardlev@gma il.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.


        Comment

        Working...