preg_replace http with https - php

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

Related

Generate secure URL external in WordPress

I'm using the function esc_url_raw..
WordPress function:
esc_url_raw( string $url, array $protocols = null )
How do I print a url with "https"?
Example:
input: http://example.com
ouput: https://example.com
Doc Function
You can provide the acceptable protocols in the $protocols variable. It can be a array() of protocols like http, https. You can refer wp_allowed_protocols for allowed protocols.
For your answer you can provide an array for $protocols as array('http', 'https') as the argument to esc_url_raw.
If you are actually looking to convert a URL from http to https then you can define something like:
function convert_to_https(url){
$new_url = preg_replace("/^http:/i", "https:", $url);
return $new_url;
}
$https_url = convert_to_https('http://example.com');
echo $https_url; // https://example.com
// then escape the URL
esc_url_raw($https_url);

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.

Grabbing digits at end of URL using PHP

On a page of mine, I have a GET as a URL of a website.
mypage.com/page.php?=URLHERE
On this URL, I need the ID at the very end of the URL
mypage.com/page.php?url=http://www.otherwebsite.com/something.php?id=%%%%%%%
These numbers are sometimes different amount of digits, so how would I do that?
My code:
$url = $_GET['url'];
Assuming the url parameter is a properly encoded URL, then useparse_url() to get the URL components and parse_str() to retrieve the id parameter from its query string.
$url = $_GET['url'];
// First parse_url() breaks the original URL up
$parts = parse_url($url);
// parse_str() parses the query string from the embedded URL
$query = parse_str($parts['query']);
// Finally, you have your full id parameter
$id = $query['id'];
assuming the the url has id at the begining of query_string
<?php
$url = $_GET['url'];
$url = basename($url);
$url =explode("?",$url);
$url = explode("=",$url[1]);
echo $url[1];
?>
try the parse_url
$url = parse_url($_GET['url'], PHP_URL_QUERY);
$query = explode('=', $url);
$id = $query[1];
I'd use the PHP function explode() http://php.net/manual/en/function.explode.php
For this example
$numbers = explode("=",$url);
$id_value = $numbers[2];
print $id_value;

PHP: Remove 'WWW' from URL inside a String

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

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