简体   繁体   English

返回URL preg_match的特定部分

[英]return specific part of url preg_match

Imagine you have this cases 想象你有这种情况

$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';

I need to get only example.com and nothing else. 我只需要获得example.com ,别无其他。 I've tried using parse_url to no avail. 我试过使用parse_url无济于事。

Using parse_url($d1, PHP_URL_HOST); 使用parse_url($d1, PHP_URL_HOST); returns nothing in $d3, for example. 例如,在$ d3中不返回任何内容。

Can any of you provide a ereg to match this? 你们任何人都可以提供一个ereg来匹配它吗?

Thank you very much in advance! 提前非常感谢您!

There is no path_url function, but you can use the parse_url function to get the host (domain name) out of a URL string: 没有path_url函数,但是您可以使用parse_url函数从URL字符串中获取主机(域名):

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

$domain = parse_url($str, PHP_URL_HOST);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM