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
某草草2017-05-16 13:16:30
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"
}
迷茫2017-05-16 13:16:30
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
巴扎黑2017-05-16 13:16:30
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];