How to remove "http://" When Echoing URL?

I'm seeking to echo the domain name (url) without the 'http://' (or 'https://').

I've created the following:

?php $surl = bloginfo('url'); $findh = array( 'http://' ); $replace = ''; $outputh = str_replace( $findh, $replace, $surl ); echo $outputh; ?

also another one (of many) I tried:

?php $surl = bloginfo('url'); echo str_replace('http://', '', $surl); ?

Seems like a simple task, but output still includes the 'http://' when the domain is echo'd. Reviewed other posts here and other sites to no avail. Perhaps something within Wordpress base files is interfering, not sure on this one.

Thanks in advance for any feedback!

Topic parse home-url site-url urls customization Wordpress

Category Web


Use this code to remove http:// and https://

$str = 'http://www.google.com';
$str = preg_replace('#^https?://#i', '', $str);
echo $str;

bloginfo echos its result, this is why your attempt to "get the value" and manipulate it results in nothing, as no value is actually being returned. If you want to get the relevant value you should use get_bloginfo instead


You could use core PHP function parse_url(); for this.

Example:

$url              = 'https://www.google.com/';
$url_data         = parse_url( $url );
$url_data['host'] = explode( '.', $url_data['host'] );
unset( $url_data['host'][0] );

echo join( '.', $url_data['host'] ); // outputs: google.com

try this may help you,

$link = get_permalink();
    $remove_http = '#^http(s)?://#';
    $remove_www  = '/^www\./';
    $replace     = '';
    $new_link    = preg_replace( $remove_http, $replace, $permalink );
    $new_link    = preg_replace( $remove_www, $replace, $new_link );
    echo '<p>' . $new_link . '</p>';

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.