UK Renault car dealers - Car Credit - Web Advertising - Loan - Web Advertising

PDA

View Full Version : Can't get regex to match properly


SGBoise
Apr 28th 2008, 8:28 pm
I am trying to match this uri. Also I want 3 to match as id and 45 in pictures45.html as pagenumber.
/category/3/pictures45.html

With this regex I got to match the category and id.
preg_match("/^\/category\/(?P<id>\d+)/", $url, $matches);

With this regex I got to match the pagenumber.
preg_match("/(?P<pagenumber>\d).html/", $url, $matches);

I figured I can combine both the regex and it should work but I can't get it to work.
preg_match("/^\/category\/(?P<id>\d+)(?P<pagenumber>\d).html/", $url, $matches);

What am I doing wrong?

Thanks guys

xrvel
Apr 28th 2008, 9:39 pm
This works but the regexp doesn't look so "good" :o

<?php
$url = '/category/3/pictures45.html';

preg_match('/\/category\/([0-9]+)\/([a-z]+)([0-9]+)/i', $url, $match);
echo('<pre>' . print_r($match, true) . '</pre>');

$id = $match[1];
$pagenumber = $match[3];

echo ('<p> ID = ' . $id . '</p>');
echo ('<p> Page number = ' . $pagenumber . '</p>');
?>

SGBoise
Apr 29th 2008, 10:22 am
xrvel, you are a genius. I have been trying for days to get it work.

Thanks for your help.