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" 
2
  • 3
    The first two work. Make sure you assign the return value to a variable, since it will not be done in place. Commented Oct 23, 2018 at 15:06
  • ^^ That. Your examples already work as you seem to want them to. Commented Oct 23, 2018 at 15:06

1 Answer 1

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;
Sign up to request clarification or add additional context in comments.

8 Comments

$url = str_replace(array('http://','https://'), '', $url); echo $url; // still yahoo.com - https:// still there
This works ok if $url set this way but not if from unserialized data? see edit to post
Well check that $website even contains anything, echo $website
Yep, it does as per post - $url leaves http:// but $url2 removes
I'm saying actually check it, or force it into string. Try var_dump($website) and show us what it says
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.