Pregunta

Quiero obtener Obtener dominio desde la URL y que se genere: http://www.domain.com/

Encontré esto, pero no sale con el http://

<?php
$url = 'http://www.lebanonpost.com/2012/05/20/press-754/';
$parse = parse_url($url);
$domain = str_ireplace('www.', '', parse_url($url, PHP_URL_HOST));
print $parse['host']; // prints 'google.com'
?>

Producción: www.lebanonpost.com

Quiero que sea: http://www.lebanonpost.com/

¿Fue útil?

Solución

intento:

print $parse['scheme'] . '://' . $parse['host'];

funcionará si hay https en lugar de http

prueba aquí

Otros consejos

puedes concatenar http:// a su salida:

<?php
    $url = 'http://www.lebanonpost.com/2012/05/20/press-754/';
    $parse = parse_url($url);
    $domain = str_ireplace('www.', '', parse_url($url, PHP_URL_HOST));
    $domainURL = $parse['scheme'].'://'.$parse['host'].'/';
    print $domainURL;
?>

This is the resource I always use for printing url's with PHP - https://stackoverflow.com/a/8891890/1964113

This answer breaks down each piece, even http/https and #fragments.

Google these things man! Really easy to find.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top