Jump to content

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.