Link to home
Start Free TrialLog in
Avatar of rdoc76
rdoc76

asked on

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:

www.mydomain.com/123456

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.

Cheers!
Avatar of leakim971
leakim971
Flag of Guadeloupe image

$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

ASKER CERTIFIED SOLUTION
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial