NYCPHP Meetup

[nycphp-talk] preg_match (related to Many pages one script discussion)

Michael B Allen ioplex at gmail.com
Mon Aug 6 21:46:36 EDT 2007


Hey,

It so happens I'm working on a CMS component and the 'Many pages one
script' discussion has inspired me to add mod_rewrite capability. I
have a catch-all RewriteRule that just sends everything through
index.php at the top of which I have a "handler table" that maps the
REQUEST_URI to a handler class. I'm using preg_match to do the match
and collect the parameters.

So an example regex looks like:

  '@/news/([0-9]+)/([0-9]+)/([0-9]+)@'

This works fine with REQUEST_URIs like:

  /news/2007/10/31

but it does not match any of the following:

  /news/2007/10
  /news/2007
  /news

I would like to match these. For example, if the day parameter is not
provided the match array returned by preg_match should simply have one
fewer elements.

So how does one designate parts of a preg_match expression as optional?

Mike



More information about the talk mailing list