我有这个正则表达式正在为网址正常工作,但是当我使用包含@的网址时,它在@之后不会显示任何内容。如何编辑正则表达式以显示完整的URL
<?php
$regex = "/(https?\:\/\/|\s)[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,4})(\/+[a-z0-9_.\:\;-]*)*(\?[\&\%\|\+a-z0-9_=,\.\:\;-]*)?([\&\%\|\+&a-z0-9_=,\:\;\.-]*)([\!\#\/\&\%\|\+a-z0-9_=,\:\;\.-]*)}*/i";
$url = "http://www.flickr.com/photos/16506140@N05/8376411748/in/photostream/";
preg_match_all($regex, $url, $matches);
echo'<pre>';
print_r($matches);
echo '<pre>';
?>
答案 0 :(得分:3)
如果您想访问网址的多个部分,我们强烈建议您使用函数parse_url()
而不是自定义正则表达式解决方案:
$url = "http://www.flickr.com/photos/16506140@N05/8376411748/in/photostream/";
var_dump(parse_url($url));
输出:
array(3) {
["scheme"]=>
string(4) "http"
["host"]=>
string(14) "www.flickr.com"
["path"]=>
string(47) "/photos/16506140@N05/8376411748/in/photostream/"
}