May 4, 2012

Get part of URL with PHP

Question by user1069269

I have a site and I would need to get one pages URL with PHP. The URL might be something or it can or it can be

So it’s always the last string but I dont want to get anything after ?

Answer by TecBrat

You will use the parse_url function, then look at the path portion of the return.
like this:


$mystring= end(explode('/',$components['path']));

Answer by Starx

parse_url() is the function you are looking for. The exact part you want, can be received through PHP_URL_PATH

$url = '';
echo parse_url($url, PHP_URL_PATH);

Author: Nabin Nepal (Starx)

Hello, I am Nabin Nepal and you can call me Starx. This is my blog where write about my life and my involvements. I am a Software Developer, A Cyclist and a Realist. I hope you will find my blog interesting. Follow me on Google+


Please fill the form - I will response as fast as I can!