Replace text between two points

i have a url e.g.
http://www.jamesbirtles.com/blog/

i want to extract “blog/” from it but the url wont always be that one e.g. it might be https://example.com/ablog/blog1/ (here i would want to extract “ablog/blog1/”). it could be any url but i want to remove “http://” or “https://” through to the first “/”

Any help would be appreciated.
Thanks

parse_url() is probably the most straightforward way.

if $string is your full URL…

$string = substr($string,strpos($string,“/”,8));

As long as your URL isnt http:///, it’ll be fine.

Greetings Manu,

You could come to a solution using several methods.

PHP allows you to use a set of “magic” arrays and the one we are particularly interested in is $_SERVER.

This array has a number of values that you might want to explorer further.

Lets start with http://, we will use getenv(‘HTTPS’) if we are using https:// then it will return ‘on’ otherwise it will return false.

$base = ( getenv(‘HTTPS’) == ‘on’ ) ? ‘https://’ : ‘http://’;

now we can see what is the “domain” that we are using, so we can use $_SERVER[‘SERVER_NAME’] which will return ‘www.domain.com’ or what ever that we want.

$base .= $_SERVER[‘SERVER_NAME’];

Next we have several options to extract the part after http://www.domain.com/ so we will use str_replace

$string_to_extract = str_replace($base,‘’,$url);

Hopefully it helps and I will be more then glad to answer further.

Kind Regards,

Mr. Alexander

Manu,

If you could be a bit more specific as to where are you getting the url variable ?

This way there might be a shortcut way of doing it as well, as i mentioned using the $_SERVER array.

Looking forward to help you,

Mr. Alexander

Wait hang on, i have just confused myself as to why i created this thread!
I just realised i just need to use this:

$_SERVER["REQUEST_URI"]

I was already using this except like this


$pageURL = 'http';
 if ($_SERVER["HTTPS"] == "on") {$pageURL .= "s";}
 $pageURL .= "://";
 if ($_SERVER["SERVER_PORT"] != "80") {
  $pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
 } else {
  $pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
 }

Wow i’m dumb