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, '//'));
Related
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¶meter=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¶meter=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¶meter=mail
dquote> freeserials.spb.ru/key/68703.htm"|grep -Po '(?<!/)([^\./]+\.[^\./]+)(?=/.+)'
output:
adwarebot.com
vistapages.com
prosearchs.com
spb.ru
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');
I need to get the ID part of a pastebin link,
which is setup like http://pastebin.com/{id}, i have tired alot of different regex i am also using preg_match in php
preg_match("~http://pastebin.com/([0-9a-zA-Z]+)~", $url, $match);
print_r($match);
or
$url = "http://pastebin.com/a65d46";
$parsed = parse_url($url);
echo trim($parsed['path'])." is ID you needed";
Instead of regex, try using parse_url to extract the path
regex would be overkill for this.
$url = "http://pastebin.com/Ugj1eqCN"
$pos = strpos($url,"pastebin.com/");
echo substr($url,$pos+13);
I need help replacing a link like google.com into http://www.google.com
$url = preg_replace("/([^\w\/])(www\.[a-z0-9\-]+\.[a-z0-9\-]+)/i", "$1http://$2",$url);
$output = htmlspecialchars(urldecode($url));
I'm using an iframe like:
<iframe src='$url'></iframe>
However, if the src="google.com" instead of "http://google.com" it will not work. So, how can I transform google.com into http://www.google.com?
Here's a non regex hack way to do it.
$url = 'google.com';
function addHTTP($url) {
return 'http://'.str_replace('http://','',$url);
}
$url = "www.google.com";
if(!preg_match("/^https/i",$url))
$url = "http://$url";
There are better ways to do this, but this will work:
if(!preg_match("#^http:\/\/#", $url)) {
$url = "http://".$url;
}
$url = 'http://' . $url;
the simpliest way possible :o
How about checking if http:// is on the beginning of it and if not tag it on? Like so:
$url = 'google.com';
if (!preg_match('#^http://#', $url)) {
$url = 'http://'.$url;
}
Just for fun, here's one that uses just preg_replace by taking advantage of a negative lookahead. However, I agree with the other solutions here, that it is probably best to just to a preg_match and then a string concatenation.
$url = preg_replace('#^(?!https?://)#', 'http://', $url);
If you just want to make your RegEx match google.com e.a., all you have to do is make www. optional. Please note that this may introduce other problems, such as end.begin being recognized as an URL.
/([^\w\/])((www\.)?[a-z0-9\-]+\.[a-z0-9\-]+)/i
i have this
http://news.google.com/news/url?sa=t&fd=R&usg=AFQjCNFyHi2aIJIV7kAlui1Sd_MQGosiBA&url=http://ksa.daralhayat.com/ksaarticle/192445
i want to get the value of url= only
$url = html_entity_decode($url);
$parts = parse_url($url);
parse_str($parts['query'],$params);
echo $params['url'];
For completeness the you can preg_match with that:
/^.*url\=(.*)$/
But you should prefer the parse_url() method that is really faster than a RegEx.