473,612 Members | 2,127 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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


Jul 17 '05 #1
6 2956
"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
>
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
.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
"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
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

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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
2199
by: pekka niiranen | last post by:
Hi there, I have perl script that uses dynamically constructed regular in this way: ------perl code starts ---- $result ""; $key = AAA\?01; $key = quotemeta $key; $line = " s^\?AAA\?01^BBB^g; #Comment "
7
2379
by: Alan Bashy | last post by:
Please, guys, In need help with this. It is due in the next week. Please, help me to implement the functions in this programm especially the first three constructor. I need them guys. Please, help me. This was inspired by Exercise 7 and Programming Problem 8 in Chapter 3 of our text. I have done Exercise 7 for you: Below you will find the ADT specification for a string of characters. It represents slightly more that a minimal string...
8
1594
by: hothead098 | last post by:
ASSIGNMENT (4) USING AND MANIPUPATING ARRAYS (Chapter 10 material) For this assignment you are to: 1) Create and manage arrays a) One of type integers (containing 10 elements). b) One of type strings (containing 20 elements). c) One of type char (containing 30 elements).
43
2215
by: ZillionDollarSadist | last post by:
Hello, I'm working at a simple Access '97 + VB4 application, and I ran into a terrible problem: something I never modified now gives me a totally unwanted "Invalid use of null" error. It happens in a Text.LostFocus event, this block: Do While Not TB6.EOF If Year(TB6(0)) = Val(Trim(Text1.Text)) Then !!!!!!!!!!!!!Here!!!!!!!!! ...
1
3703
by: Rahul | last post by:
Hi Everybody I have some problem in my script. please help me. This is script file. I have one *.inq file. I want run this script in XML files. But this script errors shows . If u want i am attach this script files and inq files. I cant understand this error. Please suggest me. You can talk with my yahoo id b_sahoo1@yahoo.com. Now i am online. Plz....Plz..Plz...
9
3219
by: quyvle | last post by:
I can't seem to get this function to work correctly. I'm wondering if anyone could help me out with this. So I'm using the fscanf function to read the input stream and store each string in the appropriate variables. Here's what I'm reading from another file: "# Number of power catergories: 9"
4
2498
by: Christian Maier | last post by:
Hi After surfing a while I have still trouble with this array thing. I have the following function and recive a Segmentation fault, how must I code this right?? Thanks Christian Maier
5
1834
by: Kelth.Raptor | last post by:
Im having some difficulty with strings here, I hope someone is kind enough to help, I do appreciate it. Im working on a grade point average calculator for my intro to programming class and I thought I would go a bit above and beyond the scope of the class and use strings. But I ran into a snag with my getgrades function. The compiler gives me the error: "81 ISO C++ forbids comparison between pointer and integer" here is the code for the...
2
10019
by: hcaptech | last post by:
This is my Test.can you help me ? 1.Which of the following statement about C# varialble is incorrect ? A.A variable is a computer memory location identified by a unique name B.A variable's name is used to access and read the value stored in it C.A variable is allocated or deallocated in memory during runtime D.A variable can be initialized at the time of its creation or later 2. The.……types feature facilitates the definition of classes...
0
8162
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8105
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8605
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8565
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
5532
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4045
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4109
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2550
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
1413
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.