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

This is a discussion on Help needed searching for a string within an array of strings... within the PHP Language forums, part of the PHP Programming Forums category; Hi, Given a string $txt and an array of strings $txt_array what would be the best/fastest way to search ...


Go Back   Usenet Forums > PHP Programming Forums > PHP Language

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 08-19-2004
Sims
 
Posts: n/a
Default 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




Reply With Quote
  #2 (permalink)  
Old 08-19-2004
steve
 
Posts: n/a
Default Re: Help needed searching for a string within an array of string

"Sims" wrote:
> 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


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
Reply With Quote
  #3 (permalink)  
Old 08-20-2004
Sims
 
Posts: n/a
Default Re: Help needed searching for a string within an array of string

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


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

Sims


Reply With Quote
  #4 (permalink)  
Old 08-20-2004
Michael Fesser
 
Posts: n/a
Default Re: Help needed searching for a string within an array of string

.oO(Sims)

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

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


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
Reply With Quote
  #5 (permalink)  
Old 08-20-2004
steve
 
Posts: n/a
Default Re: Re: Help needed searching for a string within an array of st

"Michael Fesser" wrote:
> .oO(Sims)
>
> >> foreach ($text_array as $key => $value) {
> >> if (stristr($text_array[$key], $text)) $ret_value = $key;
> >> }

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

>
> 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


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
Reply With Quote
  #6 (permalink)  
Old 08-23-2004
Christian Fersch
 
Posts: n/a
Default Re: Help needed searching for a string within an array of string

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

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


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

greetings, Christian.
Reply With Quote
  #7 (permalink)  
Old 08-24-2004
Sims
 
Posts: n/a
Default Re: Help needed searching for a string within an array of string


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


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

> greetings, Christian.


regards.

Sims


Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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




All times are GMT +1. The time now is 09:02 AM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0