这样一类的地址
abc/def/ghi/123456.shtml
abc/def/123456.shtml
abc/123456.shtml
只取出中间的数字,其它的不要,请问怎么写?

解决方案 »

  1.   


    define('PATTERN', '/(?P<path>(\w+\/)(?2)*)(?P<number>\d+)\.(?P<suffix>\w+)/');
    $array = array(
            'abc/def/ghi/123456.shtml', 
            'abc/def/ghi/123456.shtml', 
            'abc/123456.shtml'
            );  
    foreach($array as $string) {
        preg_match_all(PATTERN, $string, $matches_all, PREG_SET_ORDER);
        echo "----------------------match string: $string.-----------------------------\n";
        foreach($matches_all as $matches) {
            echo "path: ${matches['path']}, number: ${matches['number']}, suffix: ${matches['suffix']}.\n";
        }   
        echo "\n";
    }/**
    输出如下: 
    ----------------------match string: abc/def/ghi/123456.shtml.-----------------------------
    path: abc/def/ghi/, number: 123456, suffix: shtml.----------------------match string: abc/def/ghi/123456.shtml.-----------------------------
    path: abc/def/ghi/, number: 123456, suffix: shtml.----------------------match string: abc/123456.shtml.-----------------------------
    path: abc/, number: 123456, suffix: shtml. */
      

  2.   


    $str= <<<HTML
    abc/def/ghi/123456.shtml
    abc/def/123456.shtml
    abc/123456.shtml
    HTML;
    preg_match_all('/(\d+)/s',$str,$matches);
    print_r($matches[1]);Array
    (
        [0] => 123456
        [1] => 123456
        [2] => 123456
    )