Help needed searching for a string within an array of strings...

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

    #1

    Help needed searching for a string within an array of strings...

    Hi,

    Given a string $txt and an array of strings $txt_array

    what would be the best/fastest way to search in _insensitive_ case if $txt
    is in $text_array and, if it is, where is it?
    Because I want to use the array with an ID

    Something like,

    $text_array[1] = "Hello A";
    $text_array[2 = "Hello B";
    $text_array[9] = "Hello C";
    $text_array[15] = "Hello D";

    and

    $text = "hello c";

    I want to return that it was found in text_array and that the number is '9'.
    Are there any 'special' characters I need to watch out for?

    Could that be done?

    Regards.

    Sims




  • steve

    #2
    Re: Help needed searching for a string within an array of string

    "Sims" wrote:[color=blue]
    > Hi,
    >
    > Given a string $txt and an array of strings $txt_array
    >
    > what would be the best/fastest way to search in _insensitive_ case[/color]
    if[color=blue]
    > $txt
    > is in $text_array and, if it is, where is it?
    > Because I want to use the array with an ID
    >
    > Something like,
    >
    > $text_array[1] = "Hello A";
    > $text_array[2 = "Hello B";
    > $text_array[9] = "Hello C";
    > $text_array[15] = "Hello D";
    >
    > and
    >
    > $text = "hello c";
    >
    > I want to return that it was found in text_array and that the[/color]
    number[color=blue]
    > is ’9’.
    > Are there any ’special’ characters I need to watch out
    > for?
    >
    > Could that be done?
    >
    > Regards.
    >
    > Sims[/color]

    foreach ($text_array as $key => $value) {
    if (stristr($text_ array[$key], $text)) $ret_value = $key;
    }

    note: stristr in (PHP 3>= 3.0.6, PHP 4 , PHP 5)

    --
    http://www.dbForumz.com/ This article was posted by author's request
    Articles individually checked for conformance to usenet standards
    Topic URL: http://www.dbForumz.com/PHP-Help-nee...ict141272.html
    Visit Topic URL to contact author (reg. req'd). Report abuse: http://www.dbForumz.com/eform.php?p=472989

    Comment

    • Sims

      #3
      Re: Help needed searching for a string within an array of string

      >[color=blue]
      > foreach ($text_array as $key => $value) {
      > if (stristr($text_ array[$key], $text)) $ret_value = $key;
      > }
      >[/color]

      Yes, of course I know that one
      But is there not any other..., better, way?

      Sims


      Comment

      • Michael Fesser

        #4
        Re: Help needed searching for a string within an array of string

        .oO(Sims)
        [color=blue][color=green]
        >> foreach ($text_array as $key => $value) {
        >> if (stristr($text_ array[$key], $text)) $ret_value = $key;
        >> }[/color]
        >
        >Yes, of course I know that one
        >But is there not any other..., better, way?[/color]

        Not really. There are other ways, but I doubt they are better. The crux
        is that you wanna search case-insensitive, this means there has to be a
        call to a case-insensitive string comparing function somewhere. For
        case-sensitive search you could use array_search(), but there's nothing
        like array_isearch() yet.

        I would simply put the above loop in a function array_isearch() (for
        example) to have a fix API to use. Then you can play around with
        different solutions without having to change the rest of the program.

        Micha

        Comment

        • steve

          #5
          Re: Re: Help needed searching for a string within an array of st

          "Michael Fesser" wrote:[color=blue]
          > .oO(Sims)
          >[color=green][color=darkred]
          > >> foreach ($text_array as $key => $value) {
          > >> if (stristr($text_ array[$key], $text)) $ret_value = $key;
          > >> }[/color]
          > >
          > >Yes, of course I know that one
          > >But is there not any other..., better, way?[/color]
          >
          > Not really. There are other ways, but I doubt they are better. The
          > crux
          > is that you wanna search case-insensitive, this means there has to[/color]
          be[color=blue]
          > a
          > call to a case-insensitive string comparing function somewhere. For
          > case-sensitive search you could use array_search(), but there’s
          > nothing
          > like array_isearch() yet.
          >
          > I would simply put the above loop in a function array_isearch()[/color]
          (for[color=blue]
          > example) to have a fix API to use. Then you can play around with
          > different solutions without having to change the rest of the[/color]
          program.[color=blue]
          >
          > Micha[/color]

          I have never had any performance problem with php. Most problems
          relate to db access. Do you really need to squeeze php that much?

          --
          http://www.dbForumz.com/ This article was posted by author's request
          Articles individually checked for conformance to usenet standards
          Topic URL: http://www.dbForumz.com/PHP-Help-nee...ict141272.html
          Visit Topic URL to contact author (reg. req'd). Report abuse: http://www.dbForumz.com/eform.php?p=473359

          Comment

          • Christian Fersch

            #6
            Re: Help needed searching for a string within an array of string

            Sims wrote:[color=blue][color=green]
            >>foreach ($text_array as $key => $value) {
            >>if (stristr($text_ array[$key], $text)) $ret_value = $key;
            >>}[/color]
            >
            > Yes, of course I know that one
            > But is there not any other..., better, way?[/color]

            Yes, there is. I think array_walk will be faster and is in any case the
            "correct" method.

            greetings, Christian.

            Comment

            • Sims

              #7
              Re: Help needed searching for a string within an array of string

              [color=blue]
              >
              > Yes, there is. I think array_walk will be faster and is in any case the
              > "correct" method.
              >[/color]

              Sorry but i don't think array_walk is the best method.
              You cannot use it to find one string in an array, it's a callback function.

              It will always loop thru all the items regardless if an item was found.
              And storing the resuts is not the easiest/best way
              [color=blue]
              > greetings, Christian.[/color]

              regards.

              Sims


              Comment

              Working...