The code below displays: domain.com/ but I would like to display just domain.com without the / $hostname = $_SERVER['SERVER_NAME']; $hostname = str_replace('www.', '', $hostname); echo $hostname . htmlentities($_SERVER['REQUEST_URI']); Code (markup): Also how would I display domain.com in all uppercase letters; DOMAIN.COM Thanks in advance!
You may wish to use 'STRTOUPPER' function. Read about it HERE. I am not an expert in PHP but I think this code could help you: Code A: <? $hostname = $_SERVER['SERVER_NAME']; $hostname = strtoupper(str_replace('www.', '', $hostname)); echo $hostname . htmlentities($_SERVER['REQUEST_URI']); ?> PHP: Hints: Adding color and Bold is recommended. You can use the PHP or via CSS. You may use this code for better 'visibility' Example: Code A: DOMAIN.COM/myarticle.html (for URL domain.com/myarticle.html) Code B: <? $hostname = $_SERVER['SERVER_NAME']; $hostname = strtoupper(str_replace('www.', '', $hostname)); echo '<b><font color="#0000FF">'.$hostname.'</b></font>' . htmlentities($_SERVER['REQUEST_URI']); ?> PHP: Both codes above will give good result to show the real URL (with slash '/' remain intact.) Example: Code B: DOMAIN.COM/myarticle.html (for URL domain.com/myarticle.html). If you want to remove slash, you just need to remove/uncomment "['REQUEST_URI']" from your code (will only display domain name without slash. All 'complete url' will be gone). Code C: <? $hostname = $_SERVER['SERVER_NAME']; $hostname = strtoupper(str_replace('www.', '', $hostname)); $remove = array("/"); echo "".htmlspecialchars(strip_tags(str_replace($remove,"",($hostname))))." "; ?> PHP: Example: Code C: DOMAIN.COM (for whatever URL such as domain.com/myarticle.html, etc.) The code below will strip all slash '/' while keeping the url intact. This code could be optimized via merging/combining code. To strip anything, just add the words/codes in 'REMOVE'. Example, adding "/",".html" will remove '/' and '.html' completely. Code D: <? $hostname = $_SERVER['SERVER_NAME']; $hostname = strtoupper(str_replace('www.', '', $hostname)); $remove = array("/"); echo '<b><font color="#0000FF">'.htmlspecialchars(strip_tags(str_replace($remove,"",($hostname . htmlentities($_SERVER['REQUEST_URI']))))).'</b></font>'; ?> PHP: Example: Code D: DOMAIN.COMmyarticles.html (for URL domain.com/myarticle.html) From the example above, there is good reason why I recommend Code B and Code C. Code B will make the URL looks great while keeping it intact. Code C will strip '/' and discard any url (only show domain name).
Thank you Arick for providing a detailed explanation and various options. I just tried it and it worked perfectly. Now one last thing if anybody can help me with... I am trying to strip/not display the subdomain. So for example if url used to access website is http://subdomain.example.com/ is visited the code above still displays the example.domainname.com. I need to display just the domain name without the subdomain or anything else before/after it. Any thoughts/ideas? Thanks again!
You may wish to try this code: Code 1: <? $hostname = $_SERVER['SERVER_NAME']; $remove = array("www.","subdomain.","other."); $hostname = strtoupper(str_replace($remove, "", $hostname)); echo '<b><font color="#0000FF">'.$hostname . htmlentities($_SERVER).'</b></font>'; ?> PHP: Please add any of your subdomain in the '$remove' above. You may add "www.", "www1.", "download." and any subdomain which belong to the domain. Example: EXAMPLE.COM Code 2: <? $hostname = $_SERVER['SERVER_NAME']; $remove = array("www.","subdomain.","other."); $hostname = strtoupper(str_replace($remove, "", $hostname)); echo $hostname . htmlentities($_SERVER); ?> PHP: Example: EXAMPLE.COM I prefer code 3 as it is quite simple. Code 3: <? $hostname = $_SERVER['SERVER_NAME']; $remove = array("www.","subdomain.","other."); $hostname = strtoupper(str_replace($remove, "", $hostname)); echo $hostname; ?> PHP: Example: EXAMPLE.COM Alternatively, you may use the simplest code below. Code 4: <? echo 'EXAMPLE.COM'; ?> PHP: Example: EXAMPLE.COM Code 1 is good as it add color and bold. Code 2 is almost same minus bold and color. Code 4 is very simple as it use less query and didn't check anything. It only print what is available in code. Use code 4 only if the domain is already 'fixed'. I prefer Code 3 as it quite simple and could be implemented in any domain.
Thanks again... But what if I am dealing with wildcard sub-domains (unknown ones) It can be dkjfdsf.domain.com or 934843939.93938.domain.com for example... Is there a way to strip out and remove everything and just display the domain.com itself and nothing else?
Try this code: <?php $domain = $_SERVER['HTTP_HOST']; $domain = explode('.', $domain); $domain = array_reverse($domain); $domain = "$domain[1].$domain[0]"; echo strtoupper($domain); ?> PHP: I didn't test the code but I think it may work for you.
<? $server = strtoupper(str_replace('www', '', $_SERVER['SERVER_NAME'])); $bits = explode('.', $server); echo $bits[1].'.'.$bits[2]; ?> PHP: An alternative that doesnt require you to define the subdomain.
The code would work exactly as the last code I have posted above. Your code could be simplified <? $server = strtoupper($_SERVER['SERVER_NAME']); $bits = explode('.', $server); echo $bits[1].'.'.$bits[2]; ?> PHP:
Hey, No, my code doesn't do the same as yours. Yours is better. OP: Ignore everything said in the thread and stick with Arick's last code. <?php $domain = $_SERVER['HTTP_HOST']; $domain = explode('.', $domain); $domain = array_reverse($domain); $domain = "$domain[1].$domain[0]"; echo strtoupper($domain); ?> PHP: It does exactly what you need Arick: Thank you for shortening my code, sometimes i write before i think. array_reverse() is also new to me. Learn something new everyday
Better or not, it doesn't matter for me (as long as it work as expected). It is great pleasure to meet you in this forum. For OP: Use any code which suitable for you. Arick.