Jump to content

Strip subdomain from string


dreamwest

Recommended Posts

How can i strip a ".com" and the sudomain "sub." from a url

 

Ive already striped the http:// part now im just left with the rest "sub.site.com"

 


$url = parse_url("http://sub.site.com");
$display_website =  $url['host'] );

 

What i need is just the "site" part of the url

Link to comment
https://forums.phpfreaks.com/topic/159461-strip-subdomain-from-string/
Share on other sites

<?php

$url = parse_url("http://sub.site.com");
$explode = explode(".", $url['host']);
echo "Sub = " . $explode[0] . "<br />";
echo "Site = " . $explode[1] . "<br />";
echo "TLD = " . $explode[2] . "<br />";
?>

 

Thanks. this is alot simpler, i did it with strpos() but its a bit of work

 


$url = parse_url($url);
$url = $url['host'];
$url = substr($url, 0, -4); //remove .com
$url_subdomain = strpos($url, '.'); // if subdomain count characters

if ($url_subdomain !== false) {

$url_subdomain = $url_subdomain +1;
  $url = substr($url, $url_subdomain); // remove subdomain
} 

 

Think ill use explode instead

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.