473,399 Members | 3,401 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

How do I use wildcards when searching in array?

How do I use wildcards when searching in array? At least that's what I think
I need !!

I have the line:

if ($attribute[0] != "id")

The above is not 100% correct because it should also be looking for anything
ending with id. eg. bookid or authorid aswell as just id.

Should I be looking at regular expressions? I've never covered (well not
knowlingly) regular expressions before and wouldn't know one if it came up
and hit me around the face with a wet kipper.

Could someone point me in the right direction.

Cheers

Phil
Aug 9 '06 #1
10 2299
Phil Latio wrote:
Should I be looking at regular expressions? I've never covered (well not
knowlingly) regular expressions before and wouldn't know one if it came up
and hit me around the face with a wet kipper.
Yes, regular expression is arguably the most useful tool in PHP. You
wouldn't have truly mastered PHP if you hadn't mastered regexp.

The function you need is preg_grep I believe.

Aug 9 '06 #2
Phil Latio wrote:
How do I use wildcards when searching in array? At least that's what I think
I need !!

I have the line:

if ($attribute[0] != "id")

The above is not 100% correct because it should also be looking for anything
ending with id. eg. bookid or authorid aswell as just id.

Should I be looking at regular expressions? I've never covered (well not
knowlingly) regular expressions before and wouldn't know one if it came up
and hit me around the face with a wet kipper.

Could someone point me in the right direction.

Cheers

Phil
If you've described this accurately then

if (substr($attribute[0], -2) != 'id')

should work.
--
*****************************
Chuck Anderson • Boulder, CO
http://www.CycleTourist.com
*****************************
Aug 9 '06 #3
You wouldn't have truly mastered PHP if you hadn't mastered regexp.
I disagree. I have worked as a sr developer on a project written in php
and java. This project was and still is bigger than any projects I have
seen so far, that including all the open source projects. We used as
minimal of regex as possible. I personally hate regex. It is like a bad
programming practice and if you are a bit out of touch with it, its hell
to revisit the code and understand it.

I wouldn't attach that great a significance to regex.
Aug 10 '06 #4
Chung Leong wrote:
Yes, regular expression is arguably the most useful tool in PHP. You
wouldn't have truly mastered PHP if you hadn't mastered regexp.
Regular expressions are useful -- I'll give you that -- but I think it's
a little much to say that they are the _most_ useful. Their only
practical use is in highly specialized things, such as validating an
email or writing a parsing engine. My day-to-day use of regex is very
limited and I consider myself a power-user. In email validation regular
expressions make sense, but in testing if something is an integer --
that's going a little overboard.

Carl
Aug 10 '06 #5

s a n j a y wrote:
You wouldn't have truly mastered PHP if you hadn't mastered regexp.

I disagree. I have worked as a sr developer on a project written in php
and java. This project was and still is bigger than any projects I have
seen so far, that including all the open source projects. We used as
minimal of regex as possible.
Well, I guess that's precisely why it got so big in size :)
I personally hate regex. It is like a bad
programming practice and if you are a bit out of touch with it, its hell
to revisit the code and understand it.

I wouldn't attach that great a significance to regex.
Seriously now, you're entitled to have an opinion, and I respect that.
It is true that regexps can't (and shouldn't) help you much when you
need to test integers or do something like that. However, regexps *are*
the most powerfull paradigm available for handling string (as in, text)
data.

I agree it's hell to revisit code with regular expressions when you're
out of touch with it. But it's also hell to revisit code if you're out
of touch with arithmetic or boolean operators ;)

My point is, one should use the *right* (as in, the best possible) tool
for the job at hand. If you deal with text data, chances are that this
means you should use regular expressions.

You haven't truly mastered a language that gives you an opportunity to
use regexps if you can't fluently read and understand them. Just like
you haven't truly mastered a language that offers you a 'multiply'
operator, but you'd still rather use "for" loops and addition instead.
Avoiding regexps by all means sounds just like that to me.

At least this is my opinion and, once again, I respect other opinions
as well. However, I really think Chung Leong is right:
You wouldn't have truly mastered PHP if you hadn't mastered regexp.
Aug 10 '06 #6
Carl Vondrick wrote:
Regular expressions are useful -- I'll give you that -- but I think it's
a little much to say that they are the _most_ useful. Their only
practical use is in highly specialized things, such as validating an
email or writing a parsing engine. My day-to-day use of regex is very
limited and I consider myself a power-user. In email validation regular
expressions make sense, but in testing if something is an integer --
that's going a little overboard.
I think you're unduly limiting your use of regexp. Take the present
example. Is it that specialized to find a list of items matching a
particular pattern? Operations involving text strings occurs in many
places in a typical web-app. Regexp not only let you do it quickly, it
let you do it so with good tolerance for deviations. And as the pcre
functions are binary-safe, you can use them on binary data as well. The
ability to do byte-pattern matching is incredibly useful.

Aug 10 '06 #7
ninja wrote:
s a n j a y wrote:
>>You wouldn't have truly mastered PHP if you hadn't mastered regexp.
I disagree. I have worked as a sr developer on a project written in php
and java. This project was and still is bigger than any projects I have
seen so far, that including all the open source projects. We used as
minimal of regex as possible.

Well, I guess that's precisely why it got so big in size :)

>I personally hate regex. It is like a bad
programming practice and if you are a bit out of touch with it, its hell
to revisit the code and understand it.

I wouldn't attach that great a significance to regex.

Seriously now, you're entitled to have an opinion, and I respect that.
It is true that regexps can't (and shouldn't) help you much when you
need to test integers or do something like that. However, regexps *are*
the most powerfull paradigm available for handling string (as in, text)
data.
But in the case that the OP brought up:
>
>if ($attribute[0] != "id")

The above is not 100% correct because it should also be looking for anything
ending with id. eg. bookid or authorid aswell as just id.

Should I be looking at regular expressions?
Here's what the Php manual says:

"Tip: Do not use preg_match() if you only want to check if one string is
contained in another string. Use strpos() or strstr() instead as they
will be faster."

--
*****************************
Chuck Anderson • Boulder, CO
http://www.CycleTourist.com
*****************************
Aug 10 '06 #8
Chung Leong wrote:
Carl Vondrick wrote:
>>Regular expressions are useful -- I'll give you that -- but I think it's
a little much to say that they are the _most_ useful. Their only
practical use is in highly specialized things, such as validating an
email or writing a parsing engine. My day-to-day use of regex is very
limited and I consider myself a power-user. In email validation regular
expressions make sense, but in testing if something is an integer --
that's going a little overboard.


I think you're unduly limiting your use of regexp. Take the present
example. Is it that specialized to find a list of items matching a
particular pattern? Operations involving text strings occurs in many
places in a typical web-app. Regexp not only let you do it quickly, it
let you do it so with good tolerance for deviations. And as the pcre
functions are binary-safe, you can use them on binary data as well. The
ability to do byte-pattern matching is incredibly useful.
Chung,

Sorry, I have to agree with others. While I also think regex's are
important, I can think of a lot of other things which are more important.

For instance, I could argue that you aren't an expert until you
understand PHP5's classes and objects. I think a good OO structure is
more important than understand regex's.

Or I could argue that you aren't an expert until you thoroughly
understand the database interface (i.e. MySQL or PostGres) because most
applications are very dependent on databases.

Or any of a number of things.

Yes, regex's are important - but they're also specialized. Not every
program needs them, and in some cases there are better ways.

I've written a lot of code and never had a need for regex's. And I've
written code which uses regex's. But I think a higher percentage of my
code uses mysql or classes than regex's.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
Aug 11 '06 #9
Chuck Anderson wrote:
"Tip: Do not use preg_match() if you only want to check if one string is
contained in another string. Use strpos() or strstr() instead as they
will be faster."
But looping through the array and calling a function on each element
will certainly be slower than calling preg_grep() once.

Aug 11 '06 #10
Phil Latio wrote:
How do I use wildcards when searching in array? At least that's what I think
I need !!

I have the line:

if ($attribute[0] != "id")

The above is not 100% correct because it should also be looking for anything
ending with id. eg. bookid or authorid aswell as just id.
<snip>
FWIW...
<?php
$arr = array('fooid', 'foo_id', 'foo_id_xx');
print_r(array_filter($arr, create_function('$arg', 'return substr($arg,
-2)=="id";')));
?>

--
<?php echo 'Just another PHP saint'; ?>
Email: rrjanbiah-at-Y!com Blog: http://rajeshanbiah.blogspot.com/

Aug 13 '06 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Richard Berg | last post by:
Hello, I need to search a byte array for a sequence of bytes. The sequence may include wildcards. For example if the array contains 0xAA, 0xBB, 0xAA, OxDD then I want to be able to search for...
11
by: Shyguy | last post by:
I need to import a text file pretty much daily. I download the file and change the name to a standard name and then run the code to import the file into a table in my database. The problem is...
10
by: Alvaro Puente | last post by:
Hi all! Do any of you know if wildcards are accepted when calling rename() function? Thanks/Alvaro
0
by: DotNetJunkies User | last post by:
I have data in my DataTable that lists multiple values in the Roles column which are space separated like this: " AM AMB MCS DIO CO PM3 CAM " I need to be able to query on this column to get back...
3
by: Lou | last post by:
Question: I can't seem to get file.exists(filename) to return true when I search using wildcards, and I know there's a file in that dir with that extension. here's the path dim yesno as...
5
by: kenny | last post by:
I want to delete files but I also want to set the wildcards manually. Look at this example: If TextBox1.Text = "" Then Else For Each foundFile As String In...
19
by: Alan Carpenter | last post by:
Access 8 on Win98 and WinXP I'm having trouble with wildcards in the .Filename property of the FileSearch Object giving different results on win98 and XP. I've been successfully using...
5
by: nidaar | last post by:
From a security point of view, is accepting wildcards like "%" in input parameters of stored procedures against any best practices? As an example, if a user defined function uses "Productname...
5
by: lemlimlee | last post by:
hello, this is the task i need to do: For this task, you are to develop a Java program that allows a user to search or sort an array of numbers using an algorithm that the user chooses. The...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.