• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 288
  • Last Modified:

Retrieve part of a Typed URL into a variable

Hi folks, I'm in need of help with the following situation:

I'm making a php page that retrieves the number typed after my domain from the user and uses it to verify it against a certain variable.

So what I would need would be the number typed in something like the following:


Is there a way to retrieve the typed number? I want the number and the page or folder doesn't exist since it is a variable...

Any help would be greatly appreciated.

2 Solutions
$mydomain = "www.mydomain.com/123456";
$pattern = '/\D\d+/';
preg_match($pattern, $mydomain, $matches);
$number = substr($matches[0], 1);
echo $number;

Open in new window

or :
$mydomain = "www.mydomain.com/123456";
$number = explode("/", $mydomain);
echo $number[1];

Open in new window

Ray PaseurCommented:
This would imply that your server has a directory named 123456 and your script would then be the index.html inside that directory:


If you did it this way, your script could find 123456:


Example: http://www.laprbass.com/RAY_bounce_get.php?123456
Lukasz ChmielewskiCommented:
I think this would be more complicated than retrieving the value from the string.
Your address may be like www.mydomain.com/123/456/abc/def
how do you want - and what variables - to get them ?

Here's the little function

        function getMyURL(){
            $host = $_SERVER['HTTP_HOST'];
            $self = $_SERVER['PHP_SELF'];
            $par = !empty($_SERVER['QUERY_STRING']) ? $_SERVER['QUERY_STRING'] : null;
            $url = !empty($par) ? "http://$host$self?$par" : "http://$host$self";
            return $url;

Open in new window

Then you can explode it with "/" and get all variables in an array.
Everything after the host name (and port number if specified) is called the URI.  In PHP you can retrieve the URI using:


In your example  $_SERVER['REQUEST_URI'], would return "/123456".
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now