how to get certain part of url?


hi i was wondering how i can get a certain part of a url in php. For example a url like
what i want is only the part. How can i do this?

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

<!-- Sometimes it is needed to get the page name only. The following example shows how to do it: -->
function curPageName() {
 return substr($_SERVER["SCRIPT_NAME"],strrpos($_SERVER["SCRIPT_NAME"],"/")+1);

echo "The current page name is ".curPageName();

The above code displays the current url and extract the filename from the url

Also look into the manual for substr function


what i meant is getting the certain part a url from a string after a user inputs it, sorry for not mentioning earlier.



A more suitable method to look at might be parse_url.

Specifically, to get the host name out of a URL you would use:

$strUserUrl = '';
$strHost = parse_url($strUserUrl, PHP_URL_HOST);
// $strHost =

Or you could use the method without specifying a single component and it'll return an array breakdown of the URL.



thanks a lot

This article has been dead for over six months: Start a new discussion instead
Start New Discussion
Tags Related to this Article