PHP regular intercepts the content after the domain name in the URL
phpcn_u1582
phpcn_u1582 2017-05-16 13:14:30
0
5
1051

php program
needs to match the beginning of http:// or https://, retain the domain name, and intercept the content after the domain name
For example, http://www.baidu.com/aa/bbb only needs http ://www.baidu.com intercept /aa/bbb

phpcn_u1582
phpcn_u1582

reply all(5)
某草草

You don’t have to use regular rules to be generous. Isn’t this more elegant?

$url = 'http://www.baidu.com/aa/bbb';
var_dump(parse_url($url));
//array(3) {
     ["scheme"]=> string(4) "http" 
     ["host"]=> string(13) "www.baidu.com" 
     ["path"]=> string(7) "/aa/bbb"
    }
迷茫
if(strncmp('http://', $url, 7) === 0 || strncmp('https://', $url, 8)) {
  $host = substr($url, strpos($url, '/', strncmp('http://', $url, 7) ? 8 : 7));
} else {
  $host = null;
}

If possible, try not to use regular expressions

巴扎黑

There are many methods for this. Since the requirement is regular, please see the code below

    $str= 'http://www.baidu.com/aa/bbb';
    $patten = '/(http[s]?:\/\/\w*.\w*.\w*\/).*/';
    preg_match($patten, $str, $match);

    echo $match[1];
仅有的幸福

https://www.bytelang.com/o/s/...

Refer to the answers below

我想大声告诉你

Isn’t it better to use explode? Why use regular expressions

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template