0

In my custom field, users add their site URls. I want to display them like this example.com not http//example.com/. I want to remove the http/https. This is just for the display purpose.

<?php

$site = get_user_meta( $current_user->ID, 'url', true );

echo '<a class="user-website" href="'.$site.'">'.$site.'</a>';
?>

While display, it displays the entire URL like http://example.com in a link. I want to remove the protocol and display like example.com

Thanks

2 Answers 2

2
<?php

$site = get_user_meta( $current_user->ID, 'url', true );
$site_without_http = trim( str_replace( array( 'http://', 'https://' ), '', $site ), '/' );

echo '<a class="user-website" href="'.$site.'">'.$site_without_http .'</a>';

?>
Sign up to request clarification or add additional context in comments.

7 Comments

An improvement will be to add the count parameter in str_replace like this : $site_without_http = str_replace( array( 'http://', 'https://' ), '', $site,1 );
Thanks it works. Thank you but it still adds a trailing slash example.com/. Actually I added the site like this example.com
Why? I assumed the URL was valid and a valid URL can have only one complete prefix.
I have updated my answer to remove the trailing slash, if any.
Thanks once again @technoh. However, I am getting this fatal error with your updated one Fatal error: Only variables can be passed by reference in...
|
0

Using regex:

just http:// or https://

preg_replace(/\w+:\/\//, "", $string);

also www.

preg_replace(/\w+:\/\/w{3}\./, "", $string);

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.