Page 1 of 1

Getting a specific part of an url into my php script?

Posted: Wed May 23, 2007 5:40 am
by jmansa
I'm trying using "selfurl" and "basename" to get a certain part of the current url into my script, but cant figure it out... I want to get the part below marked with red into my script.
I have tryid with this, but I only get the last part of the url (welcome)

Code: Select all

function selfURL() {
	$s = empty($_SERVER["HTTPS"]) ? ''
		: ($_SERVER["HTTPS"] == "on") ? "s"
		: "";
	$protocol = strleft(strtolower($_SERVER["SERVER_PROTOCOL"]), "/").$s;
	$port = ($_SERVER["SERVER_PORT"] == "80") ? ""
		: (":".$_SERVER["SERVER_PORT"]);
	return $protocol."://".$_SERVER['SERVER_NAME'].$port.$_SERVER['REQUEST_URI'];
}
function strleft($s1, $s2) {
	return substr($s1, 0, strpos($s1, $s2));
}
$path = (selfURL());
$page = basename($path);
Can somebody please help me?!?

Posted: Wed May 23, 2007 6:55 am
by mentor
Try this solution

Code: Select all

$url = "http://www.mypage.com/index.php/en/welcome";
$last_slash = strrpos($url, "/");
$temp = substr($url, 0, $last_slash);
$second_last_slash = strrpos($temp, "/");
echo substr($url, $second_last_slash+1, $last_slash-$second_last_slash-1);

Posted: Wed May 23, 2007 7:04 am
by feyd
dirname(), basename() and parse_url() may be of interest.

Posted: Wed May 23, 2007 7:14 am
by mentor
Its easy with feyd's suggestion :wink:

Code: Select all

echo basename(dirname($url));

Posted: Wed May 23, 2007 7:23 am
by jmansa
That did the job allmost... I want the result to come out as $page
I tryid like this but that doesnt work??? My head, my head...

Code: Select all

$url = selfURL();
$last_slash = strrpos($url, "/"); 
$temp = substr($url, 0, $last_slash); 
$second_last_slash = strrpos($temp, "/"); 
$page = echo substr($url, $second_last_slash+1, $last_slash-$second_last_slash-1);
Where am I going wrong???