Can't get regex to match properly

Discussion in 'PHP' started by SGBoise, Apr 28, 2008.

  1. #1
    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
    HTML:
    With this regex I got to match the category and id.
    preg_match("/^\/category\/(?P<id>\d+)/", $url, $matches);
    PHP:
    With this regex I got to match the pagenumber.
    preg_match("/(?P<pagenumber>\d).html/", $url, $matches);
    PHP:
    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);
    PHP:
    What am I doing wrong?

    Thanks guys
     
    SGBoise, Apr 28, 2008 IP
  2. xrvel

    xrvel Notable Member

    Messages:
    918
    Likes Received:
    30
    Best Answers:
    2
    Trophy Points:
    225
    #2
    This works but the regexp doesn't look so "good" :eek:
    
    <?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>');
    ?>
    
    PHP:
     
    xrvel, Apr 28, 2008 IP
  3. SGBoise

    SGBoise Peon

    Messages:
    647
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    0
    #3
    xrvel, you are a genius. I have been trying for days to get it work.

    Thanks for your help.
     
    SGBoise, Apr 29, 2008 IP