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);
}
Related
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.
I use PHP.
I have an URL that looks like this
http://www.mydomain.com/mydir/mydir2/?something=hello
I want this:
http://www.mydomain.com
I did it like this but it feels like the wrong way to do it
To long and ugly.
$url = 'http://www.mydomain.com/mydir/mydir2/?something=hello';
$root_url_a = explode('/', $url);
$root_url = $root_url_a[0] . '//' . $root_url_a[2];
$root_url_clean = $root_url_a[2];
Suggestions
Regex?
Xpath?
Some for me unknown PHP function?
The shortest most correct way of doing it will get my vote.
Ok here is an example:
$url = parse_url('http://www.mydomain.com/mydir/mydir2/?something=hello');
echo $url->scheme.'://'.$url->host;
Is along the right lines.
Though technically this is not even right since depending on whether you send in a scheme or not for a url parse_url can actually change the way it assigns variables, so I wrote:
function return_url($url){
$parsed_url = parse_url($url);
if(!$parsed_url){
return false;
}
if(isset($parsed_url['scheme'])){
if(!isset($parsed_url['host'])){
return false;
}else{
return $parsed_url['scheme'].'://'.$parsed_url['host'];
}
}
if(isset($parsed_url['path'])){
return 'http://'.$parsed_url['path'];
}
return false;
}
I would do it like this:
$url = "http://www.mydomain.com/mydir/mydir2/?something=hello";
echo parse_url($url, PHP_URL_HOST);
// Would echo:
http://www.mydomain.com
I would say RTM ;)
http://php.net/manual/en/function.parse-url.php
<?php
$url = 'http://username:password#hostname/path?arg=value#anchor';
print_r(parse_url($url));
echo parse_url($url, PHP_URL_HOST);
?>
supposed a variable named $url,whcih value maybe $url=http://www.example.com or $url=http://example.com or $url=www.example.com
now, i want to echo the $url in www.example.com style, how to does this? thank you.
$url="http://www.example.com";
$data = parse_url($url);
echo $data["host"];
PHP parse_url: http://php.net/manual/en/function.parse-url.php
use str_replace function
echo str_replace('http://', '' , 'http://www.example.com');
$url = str_replace('http://', '' , 'http://www.example.com');
You can use strpos and str_replace to do the job. Search the http in the url through strpos if found replace with blank.
if (strpos($url,'http://') !== false)
{
$url=str_replace('http://','',$url);
}
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];
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.