Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/236.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 返回url preg_match的特定部分_Php_Regex - Fatal编程技术网

Php 返回url preg_match的特定部分

Php 返回url preg_match的特定部分,php,regex,Php,Regex,想象一下你有这个箱子 $d='http://www.example.com/'; $d1='http://example.com/'; $d2='http://www.example.com'; $d3='www.example.com/'; $d4='http://www.example.com/'; $d5='http://www.example.com/blabla/blabla.php'; 我只需要获取example.com,其他什么都不需要。 我尝试过使用解析url但没有效果 使用p

想象一下你有这个箱子

$d='http://www.example.com/';
$d1='http://example.com/';
$d2='http://www.example.com';
$d3='www.example.com/';
$d4='http://www.example.com/';
$d5='http://www.example.com/blabla/blabla.php';
我只需要获取example.com,其他什么都不需要。 我尝试过使用解析url但没有效果

使用
parse_url($d1,PHP_url_主机)在$d3中不返回任何内容

你们能提供一个ereg来匹配这个吗


提前非常感谢

没有
path\u url
函数,但您可以使用该函数从url字符串中获取主机(域名):

if(!preg_match('#^https?://#', $str))
{
    $domain = 'http://' . $domain;
}

$domain = parse_url($str, PHP_URL_HOST);

这是
PHP\u URL\u HOST
for.@BoltClock,我们可以投票编辑吗?重新编辑问题,是parse\u URL而不是path\u URL。谢谢你的回答,当然是parse\u URL。我的错。例如,在$d3中,它不返回任何内容。所以我想我应该想出一个更好的方法。@NicolásTorres:见我的最新答案。如果字符串不存在,代码将在其前面加上http://前缀。