Forum Moderators: phranque

Message Too Old, No Replies

php regex help

         

astrogirl2

1:28 pm on Aug 30, 2003 (gmt 0)

10+ Year Member



Greetings,

I have a file with entries that are URLs and site categories, for example [xxxCAR.ext,...] [somedomain...] /Shopping/Auctions/CAR/, etc. Obviously,the word "CAR" within these examples is what I am dealing with.

I need an if statement with eregi (or something like that) to determine if the string "CAR" appears in the URL or category and then do an insert of that into a database. I tried this:

bunch of code...
if(eregi("car", $directory2[i]) {
INSERT into db...
}
bunch of more code.

Problem is that all iterations of CAR, such as CAReful, CARe, sCARce, etc meet the criteria and get inserted into the db. What/how can I get only the literal "CAR" from strings like the above URL and categroy examples inserted into the db? Is there a php command that will help or do I need to get into regex?

Thank you fo your help,

Astrogirl

claus

1:43 pm on Aug 30, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



astrogirl2, i'm new to PHP, but i know about regexps and they're almost the same when you use them in PHP.

What you need is some more information about the CAR pattern. You need to analyze which specific characters are found right before and right after the specific instances of CAR that you are interested in.

You could, as an example only, use this syntax:

^CAR$

But that would only match upper-case CAR and it would need to have space on both sides of the word, so that's obviosly not an option. If you know the exact characters that would be on each side of the CAR in the cases you are interested in, ot can be matched properly, otherwise it will match all it can by default (like "careful" and such).

/claus


Added: this regexp will handle the case of "directory/CAR/page.htm"

\/CAR\/

I just found out that the "eregi" was case insensitive, so it will also match this: "directory/car/page.htm"

This will not catch the domain example though.