-1

i want to strip a url with PHP but i can't figure out how to do it. I thought about explode() but the problem is, what parameter can i use? Because i need to have every single piece of the url to use it later on in the script.

I specificaly need this parts splitted:

  1. http:// or https://
  2. www.thewebsite.com/all_the_folowing_parameters

And it also needs to work when www. is missing.

Example:

http://www.thiswebsite.com/thispage.php?this=parameter&thisis_anotherone

Then i thought i could count the characters of the url, but it could also be https, so then my plan failed.

Can anyone give me some advice? It looks that easy, but i just can't figure it out..

Rick A.
  • 213
  • 2
  • 4
  • 11

2 Answers2

3

use parse_url

print_r(parse_url('http://www.thiswebsite.com/thispage.php?this=parameter&thisis_anotherone'));

Array ( [scheme] => http [host] => www.thiswebsite.com [path] => /thispage.php [query] => this=parameter&thisis_anotherone )
Clément Baconnier
  • 5,718
  • 5
  • 29
  • 55
0

Another, less efficient way

if(substr($url, 0, 5) == "https"){
    $part1 = "https://";
    $part2 = substr($url, 8); }
else {
    $part1 = "http://";
    $part2 = substr($url, 7); }
Orry
  • 659
  • 6
  • 21