Connecting Tech Pros Worldwide Help | Site Map

How do I use wildcards when searching in array?

  #1  
Old August 9th, 2006, 03:45 PM
Phil Latio
Guest
 
Posts: n/a
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


  #2  
Old August 9th, 2006, 04:25 PM
Chung Leong
Guest
 
Posts: n/a

re: How do I use wildcards when searching in array?


Phil Latio wrote:
Quote:
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.

  #3  
Old August 9th, 2006, 07:15 PM
Chuck Anderson
Guest
 
Posts: n/a

re: How do I use wildcards when searching in array?


Phil Latio wrote:
Quote:
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
*****************************
  #4  
Old August 10th, 2006, 05:45 AM
s a n j a y
Guest
 
Posts: n/a

re: How do I use wildcards when searching in array?


Quote:
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.
  #5  
Old August 10th, 2006, 07:35 AM
Carl Vondrick
Guest
 
Posts: n/a

re: How do I use wildcards when searching in array?


Chung Leong wrote:
Quote:
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
  #6  
Old August 10th, 2006, 10:05 AM
ninja
Guest
 
Posts: n/a

re: How do I use wildcards when searching in array?



s a n j a y wrote:
Quote:
Quote:
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 :)
Quote:
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:
Quote:
You wouldn't have truly mastered PHP if you hadn't mastered regexp.
  #7  
Old August 10th, 2006, 04:05 PM
Chung Leong
Guest
 
Posts: n/a

re: How do I use wildcards when searching in array?


Carl Vondrick wrote:
Quote:
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.

  #8  
Old August 10th, 2006, 08:25 PM
Chuck Anderson
Guest
 
Posts: n/a

re: How do I use wildcards when searching in array?


ninja wrote:
Quote:
s a n j a y wrote:
>
Quote:
Quote:
>>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 :)
>
>
Quote:
>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:
Quote:
>
Quote:
>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
*****************************
  #9  
Old August 11th, 2006, 01:15 AM
Jerry Stuckle
Guest
 
Posts: n/a

re: How do I use wildcards when searching in array?


Chung Leong wrote:
Quote:
Carl Vondrick wrote:
>
Quote:
>>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.
jstucklex@attglobal.net
==================
  #10  
Old August 11th, 2006, 01:25 AM
Chung Leong
Guest
 
Posts: n/a

re: How do I use wildcards when searching in array?


Chuck Anderson wrote:
Quote:
"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.

  #11  
Old August 13th, 2006, 11:55 AM
R. Rajesh Jeba Anbiah
Guest
 
Posts: n/a

re: How do I use wildcards when searching in array?


Phil Latio wrote:
Quote:
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/

Closed Thread


Similar Threads
Thread Thread Starter Forum Replies Last Post
comp.lang.c Answers to Frequently Asked Questions (FAQ List) Steve Summit answers 0 November 14th, 2005 04:15 AM
comp.lang.c Answers to Frequently Asked Questions (FAQ List) Steve Summit answers 0 November 13th, 2005 11:37 PM
comp.lang.c Answers to Frequently Asked Questions (FAQ List) Steve Summit answers 0 November 13th, 2005 09:56 PM
comp.lang.c Answers to Frequently Asked Questions (FAQ List) Steve Summit answers 0 November 13th, 2005 03:15 AM