0

I have urls that I need to remove either http:// or https:// from the string.

So example I would like;

http://www.google.co.uk to become just www.google.co.uk
https://www.yahoo.com to become just www.yahoo.com

The string ($url) is being retrieved from serialized data in mysql database that has been unserialized into keys.

I have tried preg_replace, str_replace, parse_url to strip it from the url

preg_replace('#^https?://#', '', $url);

str_replace(array('http://','https://'), '', $url);

$url = parse_url($url);

I have searched and tried many methods but it will not be removed and the url stays the same..?

If I set string - str_replace works fine

$url = "http://www.google.co.uk"; // www.google.co.uk

It is not working from unserialized data it leaves as complete...

// $website from unserialized data - $website=$value;

$url = $website;
$url2 = "https://www.yahoo.co.uk";

$url = str_replace(array('http://',"https://"), '', $url);
$url2 = str_replace(array('http://',"https://"), '', $url2);

echo $url; // http://www.google.co.uk
echo $url2; // www.yahoo.co.uk

Value of $website

var_dump($website);  //  string(33) "http://www.google.co.uk" 
Gizz
  • 1
  • 3

1 Answers1

1

As others have said, your string works, but you need to DO the change

$url = "http://www.google.co.uk";
$url2 = "https://www.yahoo.co.uk";

$url = str_replace(array('http://',"https://"), '', $url);
$url2 = str_replace(array('http://',"https://"), '', $url2);

echo $url;
echo $url2;
clearshot66
  • 2,292
  • 1
  • 8
  • 17