问号放里面
<?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
)
<?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
)
中
由于(http:\/\/)?表示http;//可有可无
因此就用([^\/]+)来匹配<http:
我刚刚学习RE:(那
preg_match("/(http\:\/\/?)([^\/]+)/i", $a, $matches);
和
preg_match("/(http\:\/\/)?([^\/]+)/i", $a, $matches);之间或者说(x)?和(x?)的区别是什么啊?我对这块的概念有些模糊,谢谢!
preg_match("/(http)?/i", "http", $matches); 可以搜索到
preg_match("/(http)?/i","ahttp";, $matches); 死活搜索不到!!为什么嗲?
preg_match("/(http)?/i","ahttp";, $matches);
这句从头搜索,碰到a与h不符,就判断(http)?表示没有,所以就搜索不到。