NYCPHP Meetup

NYPHP.org

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

Jon Baer jonbaer at jonbaer.com
Mon Aug 6 22:17:40 EDT 2007


Ahh ... named patterns, how extremely ugly :-)

$date = "/2007/08/06";
preg_match("/(?P<year>[0-9]{4})\/(?P<month>[0-9]{2})\/(?P<day>[0-9] 
{2})/", $date, $match);
print_r($match);

- Jon

On Aug 6, 2007, at 9:56 PM, csnyder wrote:

> On 8/6/07, Michael B Allen <ioplex at gmail.com> 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
> http://chxo.com/
> _______________________________________________
> New York PHP Community Talk Mailing List
> http://lists.nyphp.org/mailman/listinfo/talk
>
> NYPHPCon 2006 Presentations Online
> http://www.nyphpcon.com
>
> Show Your Participation in New York PHP
> http://www.nyphp.org/show_participation.php




More information about the talk mailing list