问号放里面
<?phppreg_match("!(http\:\/\/?)([^\/]+)!i", "<http://www.php.net/index.html>", $matches);
$host = $matches[2];
echo $host;
print_r($matches)?>结果:www.php.netArray
(
    [0] => http://www.php.net
    [1] => http://
    [2] => www.php.net
)

解决方案 »

  1.   

    用preg_match_all/(http:\/\/)?([^\/]+)/i

    由于(http:\/\/)?表示http;//可有可无
    因此就用([^\/]+)来匹配<http:
      

  2.   

    非常感谢你
    我刚刚学习RE:(那
    preg_match("/(http\:\/\/?)([^\/]+)/i", $a, $matches);

    preg_match("/(http\:\/\/)?([^\/]+)/i", $a, $matches);之间或者说(x)?和(x?)的区别是什么啊?我对这块的概念有些模糊,谢谢!
      

  3.   


    preg_match("/(http)?/i", "http", $matches);  可以搜索到
    preg_match("/(http)?/i","ahttp";, $matches);  死活搜索不到!!为什么嗲?
      

  4.   

    (http)? 这等价于(http){0,1},即表示(http)有没有都行
    preg_match("/(http)?/i","ahttp";, $matches);  
    这句从头搜索,碰到a与h不符,就判断(http)?表示没有,所以就搜索不到。
      

  5.   

    To: sandyuk(冰の沙隆)也就是说,如果你给出的 匹配串如果 意义不确定,那么就以被搜索字符串的最先匹配结果去理解匹配串的意义?
      

  6.   

    preg_match_all("/(http)?/i","ahttp";, $matches);