0

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.

<a href="<?php echo($context['user_data']['website']) ?>" rel="nofollow" target="_blank"><?php echo $context['user_data']['website']; ?></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
    Does this answer your question? [Parsing domain from a URL](https://stackoverflow.com/questions/276516/parsing-domain-from-a-url) – Syscall Jan 09 '22 at 08:57

1 Answers1

1

You can use parse_url(), and ['host'] :

<a href="<?php echo($context['user_data']['website']) ?>" rel="nofollow" target="_blank">
    <?php echo parse_url($context['user_data']['website'])['host']; ?>
</a>

You maybe should test if parse_url() return an array before to access ['host'].

Documentation : https://www.php.net/manual/en/function.parse-url.php

$parse = parse_url($context['user_data']['website'])
echo $parse['host'].$parse['path'];
  • Sorry, but I can't seem to fit anything. If I want to display the path behind it eg example.com/example, what can I do? –  Jan 09 '22 at 09:03
  • use the result of parse_url(). It contain also ['path']. –  Jan 09 '22 at 09:06