PHP: Remove 'WWW' from URL inside a String - php

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'];

Related

Encode url in php

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

PHP preg_replace expression to remove URL parameter

I wanted to remove all occurrences of specific pattern of a parameter from a URL using preg_expression. Also removing the last "&" if exist
The pattern looks like: make=xy ("make" is fixed; "xy" can be any two letters)
Example:
http://example.com/index.php?c=y&make=yu&do=ms&r=k&p=7&
After processing preg_replace, the outcome should be:
http://example.com/index.php?c=y&do=ms&r=k&p=7
I tried using:
$url = "index.php?ok=no&make=ae&make=as&something=no&make=gr";
$url = preg_replace('/(&?lang=..&?)/i', '', $url);
However, this did not work well because I have duplicates of make=xx in the URL (which is a case that could happen in my app).
You don't need RegEx for this:
$url = "http://example.com/index.php?ok=no&make=ae&make=as&something=no&make=gr&";
list($file, $parameters) = explode('?', $url);
parse_str($parameters, $output);
unset($output['make']); // remove the make parameter
$result = $file . '?' . http_build_query($output); // Rebuild the url
echo $result; // http://example.com/index.php?ok=no&something=no
You could try using:
$str = parse_url($url, PHP_URL_QUERY);
$query = array();
parse_str($str, $query);
var_dump($query);
This will return to you the query as an array. You could then use http_build_query() function to restore the array in a query string.
But if you want to use regexp:
$url = "index.php?make=ae&ok=no&make=ae&make=as&something=no&make=gr";
echo $url."\n";
$url = preg_replace('/\b([&|&]{0,1}make=[^&]*)\b/i','',$url);
$url = str_replace('?&','?',$url);
echo $url;
This will remove all make in the URL
with rtrim you can remove last &
$url = rtrim("http://example.com/index.php?c=y&make=yu&do=ms&r=k&p=7&","&");
$url = preg_replace('~&make=([a-z\-]*)~si', '', $url);
$url = "index.php?ok=no&make=ae&make=as&something=no&make=gr";
$url = preg_replace('/(&?make=[a-z]{2})/i', '', $url);
echo $url;
Just by using preg_replace
$x = "http://example.com/index.php?c1=y&make=yu&do1=ms&r1=k&p1=7&";
$x = preg_replace(['/(\?make=[a-z]*[&])/i', '/(\&make=[a-z]*[^(&*)])/i', '/&(?!\w)/i'], ['?','',''], $x);
echo $x;
And the result is: http://example.com/index.php?c1=y&do1=ms&r1=k&p1=7
Hope this will be helpful to you guys.

PHP Get Domain With http

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.

preg_replace http with https

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

Finding Subdomain And Adding WWW with REGEX

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');

Categories