sí, creo que str_replace() y substr() son más rápidos y más limpio que la expresión regular. Aquí hay una función rápida segura para ello. Es fácil ver exactamente lo que hace. Nota: return substr ($ url, 7) y substr ($ url, 8), si también desea eliminar el //.
// slash-slash protocol remove https:// or http:// and leave // - if it's not a string starting with https:// or http:// return whatever was passed in
function universal_http_https_protocol($url) {
// Breakout - give back bad passed in value
if (empty($url) || !is_string($url)) {
return $url;
}
// starts with http://
if (strlen($url) >= 7 && "http://" === substr($url, 0, 7)) {
// slash-slash protocol - remove https: leaving //
return substr($url, 5);
}
// starts with https://
elseif (strlen($url) >= 8 && "https://" === substr($url, 0, 8)) {
// slash-slash protocol - remove https: leaving //
return substr($url, 6);
}
// no match, return unchanged string
return $url;
}
¿Por qué necesita una expresión regular? ¿Por qué no simplemente eliminar los primeros 7 caracteres? –
Verifique este: http://stackoverflow.com/questions/4875085/php-remove-http-from-link-title – stefandoorn
@OliCharlesworth: también puede contener 8 caracteres con 'https: //' – Sarfraz