Handling Identical Variables

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
BigAbe
Forum Commoner
Posts: 66
Joined: Fri Mar 31, 2006 7:41 pm

Handling Identical Variables

Post by BigAbe »

One of the programs feeding into my website is going to send me 3 of the same variables.

Example (var site):

Code: Select all

www.mysite.com/index.php?site=29&web=page123&site=A29394232&name=Bob&site=002
I know that the one I will want will have a unique identifier. In this case, the 'site' variable I want will start with A, thus the value I want is "A29394232".

How do I discern between the three variables all named the same thing?

Thanks!

-- Abe --
User avatar
PrObLeM
Forum Contributor
Posts: 418
Joined: Sun Mar 07, 2004 2:30 pm
Location: Mesa, AZ
Contact:

Post by PrObLeM »

since you can't use the $_GET['site'] because it just return the last var you can try parse_url() to get the query string, then parse it from there.
BigAbe
Forum Commoner
Posts: 66
Joined: Fri Mar 31, 2006 7:41 pm

Post by BigAbe »

PrObLeM wrote:since you can't use the $_GET['site'] because it just return the last var you can try parse_url() to get the query string, then parse it from there.
How do I get the URL? I tried

Code: Select all

<?php echo parse_url(); ?>
But that gave me
Warning: parse_url() expects exactly 1 parameter, 0 given in /home/laurels/public_html/abe/client/index3.php on line 128
User avatar
PrObLeM
Forum Contributor
Posts: 418
Joined: Sun Mar 07, 2004 2:30 pm
Location: Mesa, AZ
Contact:

Post by PrObLeM »

You need to read the manual....

Code: Select all

$array = parse_url( $urlString );
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post by Weirdan »

Code: Select all

$arr = array();
foreach(explode('&', $_SERVER['QUERY_STRING']) as $chunk) {
   $arr[] = explode('=', $chunk);
}
var_dump($arr);
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post by Weirdan »

PrObLeM wrote:You need to read the manual....

Code: Select all

$array = parse_url( $urlString );
PrObLeM, your reply wasn't particularly useful, did you mean parse_str()?
User avatar
PrObLeM
Forum Contributor
Posts: 418
Joined: Sun Mar 07, 2004 2:30 pm
Location: Mesa, AZ
Contact:

Post by PrObLeM »

Weirdan wrote:
PrObLeM wrote:You need to read the manual....

Code: Select all

$array = parse_url( $urlString );
PrObLeM, your reply wasn't particularly useful, did you mean parse_str()?
No i ment parse_url
parse_url -- Parse a URL and return its components
This function parses a URL and returns an associative array containing any of the various components of the URL that are present.
Then you need to parse $array['query']
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

viewtopic.php?t=45711 may be interesting.
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post by Weirdan »

PrObLeM wrote: [....]
Then you need to parse $array['query']
<sarcasm on>
instead of parsing $_SERVER['QUERY_STRING'] directly...
</sarcasm off>
User avatar
PrObLeM
Forum Contributor
Posts: 418
Joined: Sun Mar 07, 2004 2:30 pm
Location: Mesa, AZ
Contact:

Post by PrObLeM »

Weirdan wrote:
PrObLeM wrote: [....]
Then you need to parse $array['query']
<sarcasm on>
instead of parsing $_SERVER['QUERY_STRING'] directly...
</sarcasm off>
I never said that it was the only way, plus you are assuming that you can use the $_SERVER vars, the url maybe hardcoded in or passed from somewhere else (ie xml file, flatfile, etc).
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post by Christopher »

Here's a class method I wrote a while back for doing that:

Code: Select all

function getQueryStringParameters() {
	$params = array();
	if ($_SERVER['QUERY_STRING']) {
		$pairs = explode('&', $_SERVER['QUERY_STRING']);
		$i = 0;
		foreach($pairs as $pair) {
		   list($params[$i]['name'], $params[$i]['value']) = explode('=', $pair);
		   ++$i;
		}
	}
	return $params;
}
echo '<pre>' . print_r(getQueryStringParameters(), 1) . '</pre>';
(#10850)
BigAbe
Forum Commoner
Posts: 66
Joined: Fri Mar 31, 2006 7:41 pm

Post by BigAbe »

How could I fine tune any of these methods to take

Code: Select all

$triggerchar = "Z";
$triggervarname = "first";
and search all variables with variable name $triggervarname containing $triggerchar, then take $triggerchar and the next 8 characters to follow, and store those nine total characters as a different variable?

The reason I have to take this approach is that I'm recieving wierd output I can't control, and the exact info I want is embedded in there somewhere.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post by Christopher »

BigAbe wrote:The reason I have to take this approach is that I'm recieving wierd output I can't control, and the exact info I want is embedded in there somewhere.
Perhaps you should deal with that problem first. What kind of "wierd output I can't control"?
(#10850)
BigAbe
Forum Commoner
Posts: 66
Joined: Fri Mar 31, 2006 7:41 pm

Post by BigAbe »

arborint wrote:
BigAbe wrote:The reason I have to take this approach is that I'm recieving wierd output I can't control, and the exact info I want is embedded in there somewhere.
Perhaps you should deal with that problem first. What kind of "wierd output I can't control"?
My company is using a portal application for email, etc and it's real buggy when working with things in the outside world and incorporating them into the environment. I don't have the access level to control the output I'm provided with, so I need to work with what I have.

I wish I could be more specific, but that's all I know.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post by Christopher »

Well the code I posted would return an array of params that looked like this:

Code: Select all

// for: ?first=Zip&first=Zero&first=Xap
array(
    0 => array (
        'name' => 'first',
        'value' => 'Zip',
        ),
    1 => array (
        'name' => 'first',
        'value' => 'Zero',
        ),
    2 => array (
        'name' => 'first',
        'value' => 'Xap',
        ),
    );
So to search that for what you want would be something like:

Code: Select all

$triggerchar = "Z";
$triggervarname = "first";
$params = getQueryStringParameters();
foreach ($params as $param) {
    if (($param['name'] == $triggervarname) && (strpos($param['value'], $triggerchar) !== false)) {
        echo "found: {$param['name']}={$param['value']}";
    }
}
(#10850)
Post Reply