By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,508 Members | 1,856 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,508 IT Pros & Developers. It's quick & easy.

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

P: n/a
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


Jul 17 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
"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
Jul 17 '05 #2

P: n/a
>
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
Jul 17 '05 #3

P: n/a
.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
Jul 17 '05 #4

P: n/a
"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
Jul 17 '05 #5

P: n/a
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.
Jul 17 '05 #6

P: n/a

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
Jul 17 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.