View Full Version : getting substring name

07-20-2012, 05:57 AM

I am using `$_SERVER['REQUEST_URI'] ;` to get the complete URL address, but in that i want to take only the half, example in `$_SERVER['REQUEST_URI'] ;` i get http://www.mystore.website.com/index.php?fkdsjklfjdskldfd=id...
but i want to take only till http://www.mystore.website.com, how can i do it?

In PHP is there any function called LastIndex and Substing to use? I am confused. Please help me.

07-20-2012, 06:26 AM
$lastIndex = lastIndexOf($myURL,'/');
$myURL = substr($myURL,0,$lastIndex);
//echo $myURL;

I am using it like this. is it right?
Nothing is happening

07-20-2012, 06:40 AM
Hey, sorry for the post. I got it resolved. I was supposed to use
$lastIndex = strrpos($myURL, '/', 0); instead of
$lastIndex = lastIndexOf($myURL,'/');.

07-20-2012, 02:37 PM
Yes, but.
You'll need to determine if the results of the strrpos are valid or not. Use an if check for that:

if (false !== $lastIndex)
$myURL = substr($myURL, 0, $lastIndex);

Since 0 is a valid offset position, you must check for identical to false.