Tech Musings

Sunday, August 20, 2006

PHP Regular Expressions

I just created my first regular expression match using preg_match as I continue to work on my personal photography gallery application. My goal was to search a result for a character, in this case the forward slash (i.e. /), and fill a variable based on the result. On my details page, I wanted to check the shutter speed field in my recordset to see if the photo's speed was inputted into the record as a fraction of a second or in whole seconds. Then, based on what was in the field I'd generate a variable with text reading "th of a second" or "seconds" in the commentary paragraph on the details page.

This is a good little tutorial page I found on php regular expressions.

//checks to see if shutter speed is recorded as fraction or in full seconds

if (preg_match("%/%",$query['speed'])){$ss_terminology='th of a second';} else
{ $ss_terminology=' seconds';}

UPDATE 1/6/07!!

I needed to use preg_match on my search gallery page to add a wildcard asterick * to the end of a passed search term if someone might type "tn_filename" in the search field looking for an image they came across via the Google image search. I was running into some difficulty which prompted me to post in the PHP Freaks forum. It turns out I wasn't including delimiters!

if (preg_match("^tn%",$myword)) { do something...

should be this:

if (preg_match("/^tn%/",$myword)) { do something...

or

if (strpos($myword,"tn")===0) { do something...


0 Comments:

Post a Comment

<< Home