regex help

Discussion in 'PHP' started by linkinpark2014, Aug 8, 2009.

  1. #1
    hi
    i have this link:
    http://forum.te3p.com/314972.html
    Code (markup):
    i need to get the number before .html extension.
    so my regex pattern should get only the number..
    here is my code:

    $url = 'http://forum.te3p.com/';
    	$pattern_url = str_replace('/',"\/",$url);
    	$pattern_url = str_replace('.',"\.",$pattern_url);
    
    if(preg_match_all("/$pattern_url([0-9]*?)\.html/", $s, $t))
    
    PHP:
    any help would be appreciated...
     
    linkinpark2014, Aug 8, 2009 IP
  2. premiumscripts

    premiumscripts Peon

    Messages:
    1,062
    Likes Received:
    48
    Best Answers:
    0
    Trophy Points:
    0
    #2
    $input = 'http://forum.te3p.com/314972.html';
    $url = 'http://forum.te3p.com/';
    if (preg_match_all('/' . preg_quote($url, '/') . '([0-9]*)\.html/iU', $input, $matches, PREG_SET_ORDER)) {
    
    }
    PHP:
     
    premiumscripts, Aug 8, 2009 IP
  3. ThePHPMaster

    ThePHPMaster Well-Known Member

    Messages:
    737
    Likes Received:
    52
    Best Answers:
    33
    Trophy Points:
    150
    #3
    Use this:

    
    $url = 'http://forum.te3p.com/314972.html';
    preg_match_all('/\/([0-9]+)\./Usm',$url,$results);
    print_r($results);
    
    PHP:
     
    ThePHPMaster, Aug 8, 2009 IP
  4. jestep

    jestep Prominent Member

    Messages:
    3,659
    Likes Received:
    215
    Best Answers:
    19
    Trophy Points:
    330
    #4
    Why would you not use parse_url, and then just strip off everything else.

    $parsed_url = parse_url('http://forum.te3p.com/314972.html');

    $numbers = (int)$parsed_url['path'];

    Or if there are potentially zero's you could do.

    $numbers = substr(1,strpos('.',$parsed_url['path'] -2),$parsed_url['path']);
     
    Last edited: Aug 8, 2009
    jestep, Aug 8, 2009 IP