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 ...
|
|||||||
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
|
|||
|
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 |
|
|||
|
"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 |
|
|||
|
.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 |
|
|||
|
"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 |
|
|||
|
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. |
|
|||
|
> > 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 |