Page 1 of 1

Handling Identical Variables

Posted: Tue May 30, 2006 8:26 pm
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 --

Posted: Tue May 30, 2006 8:42 pm
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.

Posted: Tue May 30, 2006 8:54 pm
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

Posted: Tue May 30, 2006 9:14 pm
by PrObLeM
You need to read the manual....

Code: Select all

$array = parse_url( $urlString );

Posted: Tue May 30, 2006 10:05 pm
by Weirdan

Code: Select all

$arr = array();
foreach(explode('&', $_SERVER['QUERY_STRING']) as $chunk) {
   $arr[] = explode('=', $chunk);
}
var_dump($arr);

Posted: Tue May 30, 2006 10:07 pm
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()?

Posted: Tue May 30, 2006 10:11 pm
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']

Posted: Tue May 30, 2006 10:14 pm
by feyd
viewtopic.php?t=45711 may be interesting.

Posted: Tue May 30, 2006 10:48 pm
by Weirdan
PrObLeM wrote: [....]
Then you need to parse $array['query']
<sarcasm on>
instead of parsing $_SERVER['QUERY_STRING'] directly...
</sarcasm off>

Posted: Tue May 30, 2006 11:08 pm
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).

Posted: Wed May 31, 2006 12:14 am
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>';

Posted: Wed May 31, 2006 5:11 pm
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.

Posted: Wed May 31, 2006 5:29 pm
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"?

Posted: Wed May 31, 2006 5:47 pm
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.

Posted: Wed May 31, 2006 7:19 pm
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']}";
    }
}