Page 1 of 2
Can you extract variable, from string of variables?
Posted: Fri Dec 03, 2010 5:43 am
by simonmlewis
[text]$row->url = "
http://localhost/phpmyadmin/site/index. ... =&id4=&id5[/text]
This is the content of a variable from the database table.
If this was the URL, I know how simple it is to extract $id1 out.
But if it is extracted from the DB as $row->url, how do I fish out $id1, $id2 etc??
I'm sure it's one of those STR... functions, but I've no idea.
Can someone point me in the right direction please?
Ohhh - would this work?
<?php
//set string equal to Funny
$str = 'Funny';
// extract the "nn" and print
echo substr($str, 2, 2);
?>
Damn it can't as it can be 1 digit, 2 or 3 digits after id1, id2 etc.
So now can do. Still need help or advice anyone.
Re: Can you extract variable, from string of variables?
Posted: Fri Dec 03, 2010 7:32 am
by curlybracket
Code: Select all
$subject = "http://localhost/phpmyadmin/site/index.php?page=selector&menu=home&id1=397&id2=13&id3=&id4=&id5";
$pattern = '/id2=[1-9]*/';
preg_match($pattern, $subject, $matches, PREG_OFFSET_CAPTURE);
print_r($matches);
Re: Can you extract variable, from string of variables?
Posted: Fri Dec 03, 2010 8:24 am
by simonmlewis
How do I interept each "id1", "id2" result into it's own variable?
Do I say $matches1, $matches2?
Code: Select all
preg_match($pattern, $subject, $matches1, PREG_OFFSET_CAPTURE);
print_r($matches1);
So that $matches1 can be queried?
Re: Can you extract variable, from string of variables?
Posted: Fri Dec 03, 2010 9:51 am
by simonmlewis
Code: Select all
$resultwish = mysql_query ("SELECT * FROM wishlists WHERE userid = '$cookieid'");
while ($roww = mysql_fetch_object($resultwish))
{
$subject = "$roww->url";
$pattern = '/id1=[1-9]*/';
preg_match($pattern, $subject, $matches, PREG_OFFSET_CAPTURE);
print_r($matches);
} mysql_free_result($resultwish);
Produces:
[text]Array ( [0] => Array ( [0] => id1=1 [1] => 73 ) )
[/text]
Re: Can you extract variable, from string of variables?
Posted: Fri Dec 03, 2010 1:04 pm
by s992
simonmlewis wrote:
How do I interept each "id1", "id2" result into it's own variable?
Do I say $matches1, $matches2?
So that $matches1 can be queried?
$matches[1], $matches[2], etc.
Re: Can you extract variable, from string of variables?
Posted: Sun Dec 05, 2010 2:17 pm
by McInfo
Code: Select all
parse_str(parse_url($url, PHP_URL_QUERY), $vars);
PHP Manual:
Re: Can you extract variable, from string of variables?
Posted: Sun Dec 05, 2010 2:46 pm
by simonmlewis
Hi - sorry but I don't understand any of that 'parse' stuff.
Can you explain?
The matches[1] method just brings out 'array' type text on the screen.
Re: Can you extract variable, from string of variables?
Posted: Sun Dec 05, 2010 4:36 pm
by curlybracket
use print_r($array) to display array
Re: Can you extract variable, from string of variables?
Posted: Sun Dec 05, 2010 4:41 pm
by simonmlewis
Thing is, I don't want to printr the result, I was to use it, and assign it to a variable.
So that I can query a database table with $id1, $id2 etc.
Re: Can you extract variable, from string of variables?
Posted: Mon Dec 06, 2010 3:11 am
by McInfo
simonmlewis wrote:I don't understand any of that 'parse' stuff.
That is why I posted links to the manual. The manual is a great asset to people who read it.
simonmlewis wrote:I don't want to printr the result, I was to use it, and assign it to a variable.
If you don't know what is in a variable, you can't use it effectively. You use print_r() to see what is in a variable (as curlybracket said). The $vars variable I made in the "parse stuff" is the variable you should be examining.
Re: Can you extract variable, from string of variables?
Posted: Mon Dec 06, 2010 3:48 am
by simonmlewis
I'm sorry, but reading thru that, and most of it doesn't make sense to me - but it doesn't explain how to convert the content of $row->url which is 'http://*****&id=1&id2=445&id3=98..... into:
$id1 (containing 1)
$id2 (containing 445)
$id3 (containing 98)
It's simple with $id1 = $_REQUEST['id1'];, but i just don't see it with these other options.
Reading about 'parse' appears to just render things like this:
[text]<?php
$url = '
http://username:password@hostname/path?arg=value#anchor';
print_r(parse_url($url));
echo parse_url($url, PHP_URL_PATH);
?>
The above example will output:
Array
(
[scheme] => http
[host] => hostname
[user] => username
[pass] => password
[path] => /path
[query] => arg=value
[fragment] => anchor
)
/path
[/text]
Re: Can you extract variable, from string of variables?
Posted: Mon Dec 06, 2010 12:08 pm
by McInfo

The solution has been presented.
Re: Can you extract variable, from string of variables?
Posted: Mon Dec 06, 2010 1:22 pm
by simonmlewis
Only to those who know about parse etc. Which I don't. So that is a fairly sarcastic reply!!
As to me, it hasn't. Sorry.
Re: Can you extract variable, from string of variables?
Posted: Mon Dec 06, 2010 3:47 pm
by McInfo
I will try to explain the solution I posted earlier in as much detail as I can.
First, here is the solution again.
Code: Select all
parse_str(parse_url($url, PHP_URL_QUERY), $vars);
- $url is a variable that holds a string. It is the URL that comes from your database. For example, "http://localhost/phpmyadmin/site/index. ... =&id4=&id5".
- PHP_URL_QUERY is a constant that will affect the parse_url() function. The actual value of the constant does not matter to you, the programmer, but it does matter to parse_url().
- parse_url is the name of the function that will be called/executed first.
- $vars is a new, empty variable that will later hold the values we are interested in. Right now, it holds nothing.
- parse_str is the name of the function that will be called/executed second.
Now, use your imaginary magnifying glass. This is the function call that executes parse_url().
- $url and PHP_URL_QUERY are arguments being passed to the function.
- Because PHP_URL_QUERY is passed to it, parse_url() knows that it should return the query part of the given URL. So, for example, the function call is replaced with the string "page=selector&menu=home&id1=397&id2=13&id3=&id4=&id5"
Discard the magnifying glass. After parse_url() has returned a value, the original line might now look like this. This next function call executes parse_str().
Code: Select all
parse_str("page=selector&menu=home&id1=397&id2=13&id3=&id4=&id5", $vars);
- The string "page=selector&menu=home&id1=397&id2=13&id3=&id4=&id5" is what was returned by parse_url(). It is now an argument being passed to parse_str().
- $vars is an empty variable that is set for the first time on this line. As the second parameter of parse_str(), $vars will be passed by reference (rather than by value as $url and PHP_URL_QUERY were to parse_url()). That means that $vars can be changed by parse_str() and the change will remain after parse_str() is done.
- The return value of parse_str() is not useful (it is always NULL), so there is no point echoing it or capturing it with a variable. Instead, if not given a second argument, parse_str() will create variables in the current scope that have the same names as those in the given query string. However, if given a variable as the second argument (like $vars), parse_str() will instead make that variable hold an array, and fill the array with the values from the query string. The array will be associative with names from the query string as the keys.
Just for consistency, I'll show that the parse_str() function call has now been reduced to, and replaced with, this.
However, $vars was passed by reference, so it could be considered that the function call is replaced with this.
Code: Select all
$vars = array(
'page' => 'selector',
'menu' => 'home',
'id1' => '397',
'id2' => '13',
'id3' => '',
'id4' => '',
'id5' => ''
);
To see what is in the array that $vars holds, use var_dump() or print_r().
- Both print_r() and var_dump() create a string representation of the given argument and send the string directly to the output stream (most likely over HTTP to your browser window).
Calling print_r() on $vars is equivalent to this.
Code: Select all
echo "Array
(
[page] => selector
[menu] => home
[id1] => 397
[id2] => 13
[id3] =>
[id4] =>
[id5] =>
)
";
Now, you can get values from the array with array syntax. For example, to get the value "397" at key "id1",
And to see that value,
Code: Select all
var_dump($vars['id1']); // string(3) "397"
All of this (and more) can be learned from the manual.
Re: Can you extract variable, from string of variables?
Posted: Mon Dec 06, 2010 4:17 pm
by simonmlewis
Wonderfully described, thank you.
[text][id2] => 13[/text]
How do you extract that number (13) out of $vars, into one held variable?
It looks like you have to go thru hoops to extract what you want from the $url. What's a palava.
There are potentially five 'id*' in $vars, but how do you get each one (and if one is empty, how does you make the script aware?).