How to find subdomain from a url - php

URL = http://company.website.com/pages/users/add/
How do i find the subdomain from this via PHP
Such that $subdomain = 'company'
And $url = '/pages/users/add/'

You'll want to take a look at PHP's parse_url. This will give you the basic components of the URL which will make it easier to parse out the rest of your requirements (the subdomain)
$url = 'http://company.website.com/pages/users/add/';
$url_parsed = parse_url($url);
$path = $url_parsed['path']; // "pages/users/add/"
And then a simple regex* to parse $url_parsed['host'] for subdomains:
$subdomain = preg_match("/(?:(.+)\.)?[^\.]+\.[^\.]+/i", $url_parsed['host');
// yields array("company.website.com", "company")
* I tested the regex in JavaScript, so you may need to tweak it a little.

Or to avoid the regex:
$sections = explode('.', $url_parsed["host"]);
$subdomain = $sections[0];

Related

Need to change url using php

I am going to make a URL checking system.
I have this URL
https://lasvegas.craigslist.org/mob/6169799901.html
Now I want to make this URL like this
https://lasvegas.craigslist.org/search/mob?query=6169799901
how can I do it using PHP?
Since I ended up (maybe?) solving it anyways, here's one method using URL/path parsing:
$url = 'https://lasvegas.craigslist.org/mob/6169799901.html';
$parsed = parse_url($url);
$basepath = pathinfo($parsed['path']);
echo $parsed['scheme'].
"://".
$parsed['host'].
"/search".
$basepath['dirname'].
"?query=".
$basepath['filename'];
Formatted for readability.
https://3v4l.org/E6Y54
Try this
$url = "https://lasvegas.craigslist.org/mob/6169799901.html";
$id = substr($url, strrpos($url, '/') + 1);
$id = str_replace(".html","",$id);
$result = "https://lasvegas.craigslist.org/search/mob?query=".$id;
echo $result;

remove http and https from string in php

I have several urls like,
https://example.com/
http://example.com/
I only want "example.com" as string
And I want to remove the
https:// and http://
So I have taken array like this,
$removeChar = ["https://", "http://", "/"];
What is the proper way to remove these?
This worked for me,
$http_referer = str_replace($removeChar, "", "https://example.com/");
Use this php function.
Link : http://php.net/manual/en/function.parse-url.php (parse_url php function)
$url = "http://example.com/";
$domain = parse_url($url, PHP_URL_HOST);
get a result example.com
there is builtin function :
$domain = parse_url($url, PHP_URL_HOST);
try this:
$string = url ... ( your url);
$removeChar= array("http://","https://","/");
foreach($char in $removeChar)
{
$string= str_replace($char,"",$string);
}

Stripping url with preg_replace

I needed to strip the
http://www.
from a domain name and also anything following it such as
/example
so that i would just be left with yourdomain.com
I added the following code to a file:
$domain = HTTP_SERVER;
$domain_name = preg_replace('/^https?:\/\/(?:www\.)?/i', '', $domain);
But if i echo $domain_name I still get a url such as yourdomain.com/testsite
Can anyone see what i have done wrong here as it has not removed the /testsite and i thought i had got this right.
use this
$url = 'http://www.example.co.uk/directory/level1/last/page.html';
$parse= parse_url($url);
preg_match ("/\.([^\/]+)/", $parse['host'], $mydomain);
echo $mydomain[1];
This may be a hack that someone will disagree with, but i resolved the problem by using the following code.
$url = HTTP_SERVER;
$parse = parse_url($url);
$domain = $parse['host'];
$domain_name = preg_replace('/(?:www\.)?/i', '', $domain);
echo $domain_name;
If you can see a reason why this should not be used, please feel free to let me know. Always something new to learn :)

Converting incorrect url to correct url

Im am looking for a function that can convert domain.com into http://domain.com/.
Should I do this with a regex or is there a default php function which can handle this?
I have a bunch of website addresses saved mysql like this:
domain.com
www.domain.com
http://domain.com
I like to convert all of those to http://domain.com. And I am looking for a way to do this good so I won't screw up the website address.
I fixed it like this:
$url = 'domain.com';
if (strpos($url, '://') === false)
$url = 'http://' . $url;
echo $url;
based on: Validate url and convert into protocol format
You could do something like this:
$string = "http://www.domain.com";
url_fix($string);
function url_fix($str)
{
$str = str_replace(array("http://", "https://"), "", $str);
// string = www.domain.com
$str = substr_replace('www.', 0,4);
//string = domain.com
$str = "http://".$str;
//string = http://domain.com
return $str;
}
Instead of checking for both http:// and www. and doing a fancy regex for it, you could strip it of both tags (if it has it) and then just prepend http:// before the final example.com.

Is there a built in function to retrieve domain name of url in PHP?

If the url is http://www.google.com/url?sa=t&source=web&ct=res&cd=1&ved=0CAsQFjAA&url=https%3A%2F%2Fwww.google.com%2Fadsense%2F&ei=1AdLS5HSI4yQ6APt6Ly-BQ&usg=AFQjCNHKn8TzGhRO1eUfLhB79AVU-_FnGQ&sig2=EGlbrGQ3jTQdTViEt14cYg,
I need the result to be :google.com
You can use parse_url to do it like so:
$host = parse_url('http://....', PHP_URL_HOST);
$host_parts = explode('.', $host);
$domain = $host_parts[count($host_parts)-2].'.'.$host_parts[count($host_parts)-1];
That'll do it. Polish it off as you see fit.

Categories