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
Related
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, '//'));
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);
eg:
$url=http://www.example.com/.
how to make the $url to this style.
http://test.google.com/example.com in php?
PHP: Simple and easy way to format URL string should clear everything up for you
$url_parts=parse_url($url);
echo $url="http://test.google.com/".str_replace('www.','',$url_parts['host']);
$url = "http://www.example.com";
$Step1 = str_replace(array("http://", "https://", "www."), "", $url);
$Step2 = explode("/", $Step1);
$newUrl = "http://test.google.com/".$Step2[0];
Basically what I did is replacing any http://, https:// and www. strings from the URL in $url and replace them with a blank string. Then I explode the result of this replace on an '/' character, because there might be an URL given as http://www.test.com/mydir/ so we lose the mydir. If this isn't want you need, skip step 2 and replace $Step2[0] with $Step1 on the last line.
This last line adds the URL you want in $newUrl
Try this:
$url = "http://www.example.com/";
$url = preg_replace("/(?:http:\/\/)?(?:www\.)?([a-z\d-\.]+)\/.*/", "http://test.google.com/$1", $url);
Hi all i know preg_replace can be used for formatting string but i need help in that concerned area my url will be like this
http://www.example.com/index.php/
also remove the http,https,ftp....sites also
what i want is to get
result as
example.com/index.php
echo preg_replace("~(([a-z]*[:](//))|(www.))~", '', "ftp://www.example.com");
$url = 'http://www.example.com/index.php/';
$strpos = strpos($url,'.');
$output = substr($url,$strpos+1);
$parts=parse_url($url);
unset($parts['scheme']);
//echo http_build_url($parts);
echo implode("",$parts);
EDIT
To use http_build_url you needs pecl_http you can use implode as alternate
Something like this
$url = "http://www.example.com/index.php";
$parts = parse_url($url);
unset($parts['scheme']);
echo preg_replace('/^((ww)[a-z\d][\x2E])/i', '', join('', $parts));
Output
example.com/index.php
Example #2
$url = "http://ww3.nysif.com/Workers_Compensation.aspx";
Output
nysif.com/Workers_Compensation.aspx
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.