I'm validating and adding http (or https) to my URL variable with this code :
$url = preg_replace("/[^A-Za-z0-9-\/\.\:]/", "", trim($url));
$url = preg_replace('%^(?!https?://).*%', 'http://$0', $url);
But this isn't enough for me. I need one more step , too . I have to check subdomain. If there isn't any subdomain add www.
For example if there isn't any subdomain and
(after this 2 preg_replace()) if $url is : http://example.com , convert to http://WWW.example.com. If $url is : http://www.example.com, don't touch.
(with preg_replace please)
IN SUMMARY if $url hasn't subdomain and www , add www .
may be easier to use php's url parser.
http://www.php.net/manual/en/function.parse-url.php
I got this:
$url = 'http://teknoblogo.com';
$host = parse_url($url, PHP_URL_HOST);
$arr = explode('.', $host);
echo http_build_url($url,
array(
'host' => !preg_match('/^www\d*\.$/i', $arr[0]) && count($arr) <= 2 ? 'www.' . $host : $host
)
);
See also:
parse_url()
http_build_url()
Without TLD lookup tables, the only way I imagine you can do this is if you know your domain already:
$domain = 'example.com';
$url = preg_replace('~^(https?://)(' . preg_quote($domain, '~') . ')(.*)~i', '$1www.$2$3');
Related
i am passing a url as a param to the next page. ?url=http://domain.com i would like to set additional param to a querystring or the url. but only if a specific domain exists in the querystring.
i tried
$url = preg_replace('{http://www.domain.com}','http://www.domain.com?foo=bar/',$_GET['url']);
but this is not working when there is a file name or other params.
any help is appreciated.
This will do what you want:
$url = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]"; // the URL you want to inject the parameters into
$params = "new=yes!"; // the new parameters you want to add at the beginning
if (strpos($url, "?") !== false) {
list($url, $b) = explode("?", $url, 2);
$params = "$params&$b";
}
$url .= "?".$params;
Output: http://example.com/example.php?new=yes!&a=b&c=no
If you want to place some parameter to the beginning of the query string you can use parse_url function:
$url = "http://" . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
$parsed_url = parse_url($url);
$new_url = $parsed_url['path'] ."?foo=bar" . ((isset($parsed_url['query']))? urlencode("&").$parsed_url['query'] : "");
var_dump($new_url);
// the output: string(45) "http://www.domain.com?foo=bar%26param=value"
I have string url variable;
$url ="http://carkva-gazeta.org/римско-католическая-церковь/";
I need transform $url to:
"http://carkva-gazeta.org/%d1%80%d0%b8%d0%bc%d1%81%d0%ba%d0%be-%d0%ba%d0%b0%d1%82%d0%be%d0%bb%d0%b8%d1%87%d0%b5%d1%81%d0%ba%d0%b0%d1%8f-%d1%86%d0%b5%d1%80%d0%ba%d0%be%d0%b2%d1%8c/"
I have tried: rawurlencode($url);
and urlencode($url);
But result is:
http%3A%2F%2Fcarkva-gazeta.org%2F%D1%80%D0%B8%D0%BC%D1%81%D0%BA%D0%BE-%D0%BA%D0%B0%D1%82%D0%BE%D0%BB%D0%B8%D1%87%D0%B5%D1%81%D0%BA%D0%B0%D1%8F-%D1%86%D0%B5%D1%80%D0%BA%D0%BE%D0%B2%D1%8C%2F
$url = "http://carkva-gazeta.org/";
$url .= urlencode("римско-католическая-церковь");
echo $url;
Like so?
Probably that's the best solution:
$url ="http://carkva-gazeta.org/римско-католическая-церковь/";
$x = parse_url($url);
echo $x['scheme'].'://'.$x['host'].strtolower(str_replace('%2F','/',urlencode($x['path'])));
I've used also strtolower to make it lowercase as you wanted
Assuming that you are getting your URL string automatically/dynamically and that it is not a fixed string that you can simply split while writing your code, you'll want something like this
$url = "http://carkva-gazeta.org/римско-католическая-церковь/";
// in case it is https, we don't want to hardcode http
$scheme = parse_url($url, PHP_URL_SCHEME);
$host = parse_url($url, PHP_URL_HOST);
// do not encode the first '/' or the last '/'
$encodedPath = strtolower(urlencode(substr(parse_url($url, PHP_URL_PATH), 1, -1)));
$encodedUrl = $scheme . "://" . $host . "/" . $encodedPath . "/";
DEMO
I want to get Get Domain from URL and be output: http://www.domain.com/
I found this, but does not come out with the http://
<?php
$url = 'http://www.lebanonpost.com/2012/05/20/press-754/';
$parse = parse_url($url);
$domain = str_ireplace('www.', '', parse_url($url, PHP_URL_HOST));
print $parse['host']; // prints 'google.com'
?>
Output: www.lebanonpost.com
I want it to be: http://www.lebanonpost.com/
Try:
print $parse['scheme'] . '://' . $parse['host'];
It will work if there is https instead of http
Test Here
You can concate http:// to your output:
<?php
$url = 'http://www.lebanonpost.com/2012/05/20/press-754/';
$parse = parse_url($url);
$domain = str_ireplace('www.', '', parse_url($url, PHP_URL_HOST));
$domainURL = $parse['scheme'].'://'.$parse['host'].'/';
print $domainURL;
?>
This is the resource I always use for printing url's with PHP - https://stackoverflow.com/a/8891890/1964113
This answer breaks down each piece, even http/https and #fragments.
Google these things man! Really easy to find.
Put simply, I need to check if the string in the variable $url is a simple http, if so, replace it with https - but I can't get it to work - any ideas:
$url="http://www.google.com"; // example http url ##
$url_replaced = preg_replace( '#^http://#','https://', $url ); // replace http with https ##
Cheers!
Why not str_replace ?
$url="http://www.google.com"; // example http url ##
$url = str_replace('http://', 'https://', $url );
echo $url;
preg_replace() is unnecessary here. Just use str_replace().
str_replace('http://', 'https://', $url)
You could always create a simple function that returns the link as secure. Much easier if you need to change a lot of links.
function secureLink($url){
$url = str_replace('http://', 'https://', $url );
return $url;
};
Do NOT use str_replace, as it can happen you will replace string in the middle (if the url is not encoded correctly).
preg_replace("/^http:/i", "https:", $url)
Note the /i parameter for case insensitive and ^ saying it have to start with this string.
http://sandbox.onlinephpfunctions.com/code/3c3882b4640dad9b6988881c420246193194e37e
Currently I am using parse_url, however the host item of the array also includes the 'WWW' part which I do not want. How would I go about removing this?
$parse = parse_url($url);
print_r($parse);
$url = $parse['host'] . $parse['path'];
echo $url;
$url = preg_replace('#^www\.(.+\.)#i', '$1', $parse['host']) . $parse['path'];
This won't remove the www in www.com, but www.www.com results in www.com.
preg_replace('#^(http(s)?://)?w{3}\.#', '$1', $url);
if you don't need a protocol prefix, leave the second parameter empty
$url = preg_replace('/^www\./i', '', $parse['host']) . $parse['path'];