5

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!

4 Answers 4

5

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

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

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

1
  • Perfect! - (and shaking head why I didn't pick up on that after trying 'get_site_url' attempt). Thank you Mark!
    – Dan
    Sep 19, 2017 at 6:12
3

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
1
  • 1
    Just be sure that the first part of the url is "www", otherwise you're stripping a sensible part from the url: https://wordpress.stackexchange.com/ would return stackexchange.com (anyway, the OP didn't say anything about the "www", so I would not remove it) :-)
    – Erenor Paz
    Apr 29, 2021 at 12:41
0

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>';
3
  • Thanks Vikas for your feedback. I've tried using the 'preg_replace' before with no success. I did try your suggestion using the 'get_site_url' hook to no avail receiving the "Notice: Undefined variable: permalink in ..." message (like before when using the preg_replace"). Thanks again for your suggestion Vikas!
    – Dan
    Sep 18, 2017 at 8:30
  • Welcome !! @Dan Sep 18, 2017 at 9:06
  • If you don't need complex regular expressions, you're better off using str_replace() instead of preg_replace(). You could, for example, replace this code with $new_link = str_replace( array( 'http://', 'https://', 'www.' ), array( '', '', '' ), get_permalink() ); echo "<p>$new_link</p>"; .
    – Pat J
    Sep 18, 2017 at 15:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.