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

csnyder chsnyder at
Mon Aug 6 21:56:38 EDT 2007

On 8/6/07, Michael B Allen <ioplex at> wrote:
> 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

How optional? Why use preg_match at all?

list( $app, $year, $month, $day ) = explode( '/', $uri );

Chris Snyder

More information about the talk mailing list