I have written a PHP code that fetches website images from my other website (both are on separate hosting)
$listings['pic'][$count] = $critem[2];
The $critem[2]
sometimes returns either 1 or 2
1. https://www.example.com/wp-content/plugins/featured-content-gallery/scripts/mootools.v1.11.js
2. /wp-content/uploads/2017/07/Sidetracks-Main-1.jpg
What I am trying to do is Find
http://www.example.com
and replace with https://www.example.com
and if there is no domain in the $critem[2]
as in the (2nd example) append https://www.example.com
to the string
I am stuck as str_replace
does not work in this criteria
Something like this?
$arr = ["http://www.example.com//wp-content/uploads/2017/07/Sidetracks-Main-1.jpg", "/wp-content/uploads/2017/01/45432.jpg", "/wp-content/uploads/2016/07/39223.jpg"];
foreach($arr as &$link){
$link = str_replace("http://", "https://", $link);
if(substr($link,0,8) != "https://") $link = "https://www.example.com" . $link;
}
Var_dump($arr);
It replaces http with https if it's in the string.
And if the link does not have the http, it adds it with the str_replace
https://3v4l.org/G0AiQ