Remove http from variable - php

I have a variable, such as this:
$domain = "http://test.com"
I need to use preg_replace or str_place to get the variable like this:
$domain = "test.com"
I have tried using the following, but they do not work.
1) $domain = preg_replace('; ((ftp|https?)://|www3?\.).+? ;', ' ', $domain);
2) $domain = preg_replace(';\b((ftp|https?)://|www3?\.).+?\b;', ' ', $domain);
Any suggestions?

Or you can use parse_url:
parse_url($domain, PHP_URL_HOST);

$domain = ltrim($domain, "http://");

Did you try the str_replace?
$domain = "http://test.com"
$domain = str_replace('http://','',$domain);
You regular expressions probably don't find a match for the pattern.

preg_replace('~(https://|http://|ftp://)~',, '', $domain);

preg_match('/^[a-z]+:[/][/](.+)$/', $domain, $matches);
echo($matches[1]);
Should be what you are looking for, should give you everything after the protocol... http://domain.com/test becomes "domain.com/test". However, it doesn't care about the protocol, if you only want to support specific protocols such as HTTP and FTP, then use this instead:
preg_match('/^(http|ftp):[/][/](.+)$/', $domain, $matches);
If you only want the domain though, or similar parts of the URI, I'd recommend PHP's parse_url() instead. It does all the hard work for you and does it the proper way. Depending on your needs, I would probably recommend you use it anyway and just put it all back together instead.

simple regex:
preg_replace('~^(?:f|ht)tps?://~i','', 'https://www.site.com.br');

Related

How do I remove http, https from string in php

Example user input
http://domain.com/
hTTp://domain.com/Cars/
hTtp://www.domain.com/pAge/
I want a php function to make the output like
domain.com
domain.com/Cars/
www.domain.com/pAge/
Let me know :)
You don't need regular expressions here, just use parse_url and str_replace:
$url = 'hTtp://www.domain.com/pAge/';
$url = str_replace( parse_url( $url, PHP_URL_SCHEME ) . '://', '', $url );
Consider using parse_url() to get an array with the different parts of the url and rebuild it as a string any way you want.
Consider using a regex, with preg_replace
$converted = preg_replace('#^h+t+p+s+?://#i', '', $stringtoprocess);
Maybe the easiest way might be
echo str_replace('//','',strstr($url, '//'));

Get domain name without www and .com in PHP

I need to get domain name from URL excluding "www" and ".com" or ".co.uk" or anything other.
Example-
I have following urls like-
http://www.example.com
http://www.example.co.uk
http://subdomain.example.com
http://subdomain.example.co.uk
There will be anything at ".com" , ".org" , ".co.in", ".co.uk".
I try this it work for me.
$original_url="http://subdomain.example.co.uk"; //try with all urls above
$pieces = parse_url($original_url);
$domain = isset($pieces['host']) ? $pieces['host'] : '';
if (preg_match('/(?P<domain>[a-z0-9][a-z0-9\-]{1,63}\.[a-z\.]{2,6})$/i', $domain, $regs)) {
echo strstr( $regs['domain'], '.', true );
}
Output- example
I get this from Here
Get domain name from full URL
(?:https?:\/\/)?(?:www\.)?(.*)\.(?=[\w.]{3,4})
Try this.See demo.Grab the capture.
http://regex101.com/r/bW3aR1/2
You should use the PHP function parse_url() in combination with a str_replace() or regex, or maybe even an explode. It depends on a few things:
Things to note:
Will there always be a subdomain?
Will there be a specific list of allowed subdomains?
I would do something like this:
<?php
$url = 'http://www.something.com';
$parts = explode('.', parse_url($url, PHP_URL_HOST));
echo $parts[1]; // "something"

Using rtrim in php for specific purpose

I am not much used to using rtrim and Reg expressions. So I wanted to get my doubt cleared about this:
Here is a url: http://imgur.com/r/pics/paoWS
I am trying to use rtrim function on this url to pick out only the 'paoWs' from the whole url.
Here is what i tried:
$yurl = 'http://imgur.com/r/pics/paoWS';
$video_id = parse_url($yurl, PHP_URL_PATH);
$yid=rtrim( $video_id, '/' );
And i am using '$yid' to hotlink the image from imgur. But What I get after trying this function is:
$yid= '/r/pics/paoWS'
How do I solve this?
rtrim is used for trimming down a string of certain characters or whitespace on the right-hand side. It certainly shouldn't be used for your purpose.
Assuming the URL structure will always be the same, you could just do something like this:
$yurl = 'http://imgur.com/r/pics/paoWS';
$video_id = parse_url($yurl, PHP_URL_PATH);
$parts = explode('/', $video_id)
$yid = end($parts);
You sould not use regular expressions (whitch are 'expensive') for a so 'simple' problem.
If you want to catch the last part of the URL, after the last slash, you can do :
$urlParts = explode('/', 'http://imgur.com/r/pics/paoWS');
$lastPart = end($urlParts);
rtim( strrchr('http://imgur.com/r/pics/paoWS' , '/') ); rtrim + strrchr
substr(strrchr('http://imgur.com/r/pics/paoWS', "/"), 1); substr + strrchr
rtrim() returns the filtered value, not the stripped characters. And your usage of it isn't proper too - it strips the passed characters from the right side. And you don't need parse_url() either.
Proper answers have been given already, but here's a faster alternative:
$yid = substr($yurl, strrpos($yurl, '/')+1);
Edit: And another one:
$yid = ltrim(strrchr($yurl, '/'), '/');

Preg_replace domain problem

I'm Stuck try to get domain using preg_replace,
i have some list url
download.adwarebot.com/setup.exe
athena.vistapages.com/suspended.page/
prosearchs.com/se/tds/in.cgi?4&group=5&parameter=mail
freeserials.spb.ru/key/68703.htm
what i want is
adwarebot.com
vistapages.com
prosearchs.com
spb.ru
any body can help me with preg_replace ?
i'm using this http://gskinner.com/RegExr/ for testing :)
using preg_replace, if the number of TLDs is limited:
$urls = array( 'download.adwarebot.com/setup.exe',
'athena.vistapages.com/suspended.page/',
'prosearchs.com/se/tds/in.cgi?4&group=5&parameter=mail',
'freeserials.spb.ru/key/68703.htm' );
$domains = preg_replace('|([^.]*\.(?:com|ru))/', '$1', $urls);
matches everything that comes before .com or .ru which is not a period. (to not match subdomains)
You could however use PHPs builtin parse_url function to get the host (including subdomain) – use another regex, substr or array manipulation to get rid of it:
$host = parse_url('http://download.adwarebot.com/setup.exe', PHP_URL_HOST);
if(count($parts = explode('.', $host)) > 2)
$host = implode('.', array_slice($parts, -2));
Following code assumes that every entry is exactly at the beginning of the string:
preg_match_all('#^([\w]*\.)?([\w]*\.[\w]*)/#', $list, $m);
// var_dump($m[2]);
P.S. But the correct answer is still parse_url.
Why use a regular expression? Of course it is possible, but using this:
foreach($url in $url_list){
$url_parts = explode('/', $url);
$domains[] = preg_replace('~(^[^\.]+\.)~i','',$url_parts[0]);
}
$domains = array_unique($domains);
will do just fine;
maybe a more generic solution:
tested by grep, I don't have php environment, sorry:
kent$ echo "download.adwarebot.com/setup.exe
dquote> athena.vistapages.com/suspended.page/
dquote> prosearchs.com/se/tds/in.cgi?4&group=5&parameter=mail
dquote> freeserials.spb.ru/key/68703.htm"|grep -Po '(?<!/)([^\./]+\.[^\./]+)(?=/.+)'
output:
adwarebot.com
vistapages.com
prosearchs.com
spb.ru

Regular expression to get the main domain of a URL

I have never used regex before and I was wondering how to write a regular expression in PHP that gets the domain of the URL. For example:
http://www.hegnar.no/bors/article488276.ece --> hegnar.no
You dont need to use regexp for this task.
Check PHP's built in function, parse_url
http://php.net/manual/en/function.parse-url.php
Just use parse_url() if you are specifically dealing with URLs.
For example:
$url = "http://www.hegnar.no/bors/article488276.ece";
$url_u_want = parse_url($url, PHP_URL_HOST);
Docs
EDIT:
To take out the www. infront, use:
$url_u_want = preg_replace("/^www\./", "", $url_u_want);
$page = "http://google.no/page/page_1.html";
preg_match_all("/((?:[a-z][a-z\\.\\d\\-]+)\\.(?:[a-z][a-z\\-]+))(?![\\w\\.])/", $page, $result, PREG_PATTERN_ORDER);
print_r($result);
$host = parse_url($url, PHP_URL_HOST);
$host = array_reverse(explode('.', $host));
$host = $host[1].'.'.$host[0];
See
PHP Regex for extracting subdomains of arbitrary domains
and
Javascript/Regex for finding just the root domain name without sub domains
This is the problem when you use parse_url, the $url with no .com or .net or etc then the result returned is bannedadsense, this mean returning true, the fact bannedadsense is not a domain.
$url = 'http://bannedadsense/isbanned'; // this url will return false in preg_match
//$url = 'http://bannedadsense.com/isbanned'; // this url will return domain in preg_match
$domain = parse_url($url, PHP_URL_HOST));
// return "bannedadsense", meaning this is right domain.
So that we need continue to check more a case with no dot extension (.com, .net, .org, etc)
if(preg_match("/^[a-zA-Z0-9][a-zA-Z0-9-]{1,61}[a-zA-Z0-9](?:\.[a-zA-Z]{2,})+$/i",$domain)) {
echo $domain;
}else{
echo "<br>";
echo "false";
}

Categories