1

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!

3

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

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

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
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>';
  • 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 '17 at 8:30
  • Welcome !! @Dan – UltimateDevil Sep 18 '17 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 '17 at 15:39
0

Use this code remove http://

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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