need to find out if string contains a list of words

Any questions involving matching text strings to patterns - the pattern is called a "regular expression."

Moderator: General Moderators

Post Reply
Extremest
Forum Commoner
Posts: 84
Joined: Mon Aug 29, 2005 12:39 pm

need to find out if string contains a list of words

Post by Extremest »

I have a list of urls. I need to check the them to see if they contain .jpg or .png stuff like that. So that i can skip them. So like an if with that as the expression for the if. If it doesn't contain them I can have them put into the db. Can anyone please help me with this.
IAD
Forum Commoner
Posts: 42
Joined: Wed Dec 28, 2005 12:36 pm
Location: Israel, Soon Canada :)

Post by IAD »

Code: Select all

if(eregi(".jpg",$your_var)){

action

}
?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

there's no real need for a regex.. strpos() would work faster and be easier to understand, likely..
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post by Jenk »

and also using ereg* as opposed to preg* is less efficient as well.
Extremest
Forum Commoner
Posts: 84
Joined: Mon Aug 29, 2005 12:39 pm

Post by Extremest »

ok strpos works. Now here is a dumb question. How can I have it check for multiple needles. Like check for .jpg,.jpeg,.bmp,.png etc. All of them at once and if it contains none of them then it can write.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

for all that once, that is best served by a regular expression.

/\.(jpe?g|bmp|png|gif)/

for example.
foobar
Forum Regular
Posts: 613
Joined: Wed Sep 28, 2005 10:08 am

Post by foobar »

feyd wrote:for all that once, that is best served by a regular expression.

/\.(jpe?g|bmp|png|gif)/

for example.
Yea, or just cycle through them and put it in strpos() every time.
Extremest
Forum Commoner
Posts: 84
Joined: Mon Aug 29, 2005 12:39 pm

Post by Extremest »

ok so use preg_match with that regex and if it === 0 then it did not contain it and then write right?
Extremest
Forum Commoner
Posts: 84
Joined: Mon Aug 29, 2005 12:39 pm

Post by Extremest »

ok that works great thank you very much.
Extremest
Forum Commoner
Posts: 84
Joined: Mon Aug 29, 2005 12:39 pm

Post by Extremest »

just one quick question about that regex. Does this use the "." or jsut the jpg and that.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

it requires a dot and jpeg, jpg, bmp, png, or gif to follow.
Extremest
Forum Commoner
Posts: 84
Joined: Mon Aug 29, 2005 12:39 pm

Post by Extremest »

ok just wanted to make sure. I have everything working so far in my spider but for some reason apache will crash after a little while with a memory read error. Also get a warning on my index value in an array. Will figure those arrays out gradually.
Post Reply