我通过这种方式从表单中获取 url:
$input_website = isset($_POST['website']) ? check_plain($_POST['website']) : 'None';
我需要取回一个裸域名(用于某些 API 集成),例如:http ://www.example.com将返回为example.com ,www.example.com将返回example.com等。
我现在有这段代码,它为第一种情况 http://www.example.com返回正确的 url,但不返回www.example.com甚至example.com:
function get_domain($url)
{
$pieces = parse_url($url);
$domain = isset($pieces['host']) ? $pieces['host'] : '';
if (preg_match('/(?P<domain>[a-z0-9][a-z0-9\-]{1,63}\.[a-z\.]{2,6})$/i', $domain, $regs)) {
return $regs['domain'];
}
return false;
}
你能就此事提出建议吗?