• Hello!

    I have a wordpress page where I use custom post fields for each post containing details of the post
    (one of them is URL of the source).

    I call the values to the frontpage with the following code:
    <?php $key = "source_url"; echo get_meta($key); ?>

    I would like to get only domain from this whole URL and put the whole URL in the link.

    Example: https://www.google.com/blablabla/link215616.html would be shown as google.com and on click you would be dirrected to https://www.google.com/blablabla/link215616.html.

    I was looking a lot on the web and trying lots of stuff but I’m not so experienced with PHP and I have problems to figure it out.

    any suggestions?

Viewing 4 replies - 1 through 4 (of 4 total)
  • <?php
    $url = get_meta("source_url");
    $pos = strpos($url, '/', 7);
    $domain = substr($url, 0 ,$pos);
    print "<a href='$url'>$domain</a>";
    ?>

    Search Google for: String PHP

    You get to this site, all the functions I have used are described there.
    https://php.net/manual/en/book.strings.php

    Thread Starter davorin

    (@davorin)

    Excellent!!
    That did the trick ??

    Thank you very much!

    Is there any way to also remove the https:// and www in front?

    Yep, change the 0 in the script and make it 11. That’s the variable of the first character of the domain name. It starts counting at 0, and https://www. is 11 characters long.

    Good luck!

    Thread Starter davorin

    (@davorin)

    It doesnt work as I want.. Now it doesnt strips the code after the domain ending (doesnt end with .com for instance but it goes further for 11 characters).. and also it doesnt work right if there is no www in front..

    I really appreciate your help. Thank you

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Get domain name from URL’ is closed to new replies.