Help on 'get only a certain part of a variable'

Ask about general coding issues or problems here.

Moderators: egami, macek, gesf

Post Reply
User avatar
silhouett
New php-forum User
New php-forum User
Posts: 8
Joined: Mon Jul 18, 2011 3:40 pm
Contact:

Tue Jul 19, 2011 7:10 am

I use the following commands in php

$myurl="http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
echo $myurl;


what I get back is http://mysite.com/index.php?id=195

how can I in php only get the part after the id that is a number ?


Thanks in advance :?

User avatar
gandahar
New php-forum User
New php-forum User
Posts: 112
Joined: Tue Mar 15, 2011 4:05 am

Wed Jul 20, 2011 12:19 am

easy :

Code: Select all

$myVar= $_GET['id'];
$myVar = 195

User avatar
silhouett
New php-forum User
New php-forum User
Posts: 8
Joined: Mon Jul 18, 2011 3:40 pm
Contact:

Wed Jul 20, 2011 7:54 am

Although those answers are ok they wont do what I am looking to do. I need to take a variable that contains "http://mywebsite.com?id=195" and get what the user typed in the url not what id equals
as ID changes because of other processes in the webpage.

How do I search for what ever number is after the id field in the above string ?

User avatar
Nullsig
php-forum Fan User
php-forum Fan User
Posts: 979
Joined: Thu Feb 17, 2011 6:52 am
Location: Racine, WI

Wed Jul 20, 2011 8:36 am

Are you suggesting that the "id" variable will be a mixed alphanumeric string?

In which case are the numbers you want:
1. Mixed at random within the string
This will require a regular expression or loop to extract the data... but you will still use $_GET

2. The first group of numbers reached?[i.e. xsd1234zxssd342ds evaluates to 1234]
This requires a loop ... but you will still use $_GET

3. A chunk of number that will always be the furthest left characters in a string [i.e. 1234azxic324 returns 1234]
This can be accomplished with intval() ... but you will still use $_GET

4. A chunk of numbers at the end of the string
This can be accomplished with a loop ... but you will still use $_GET


There are numerous other possibilities but in the end you will still use $_GET

User avatar
silhouett
New php-forum User
New php-forum User
Posts: 8
Joined: Mon Jul 18, 2011 3:40 pm
Contact:

Wed Jul 20, 2011 8:42 am

Thank You that worked great! :D

Post Reply