Can you extract variable, from string of 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

simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Can you extract variable, from string of variables?

Post 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.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
curlybracket
Forum Commoner
Posts: 59
Joined: Mon Nov 29, 2010 2:40 pm

Re: Can you extract variable, from string of variables?

Post 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);
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: Can you extract variable, from string of variables?

Post by simonmlewis »

$subject = "http://localhost/phpmyadmin/site/index. ... =&id4=&id5";
$pattern = '/id2=[1-9]*/';
preg_match($pattern, $subject, $matches, PREG_OFFSET_CAPTURE);
print_r($matches);
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?
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: Can you extract variable, from string of variables?

Post 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]
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
s992
Forum Contributor
Posts: 124
Joined: Wed Oct 27, 2010 3:06 pm

Re: Can you extract variable, from string of variables?

Post 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.
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

Re: Can you extract variable, from string of variables?

Post by McInfo »

Code: Select all

parse_str(parse_url($url, PHP_URL_QUERY), $vars);
PHP Manual:
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: Can you extract variable, from string of variables?

Post 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.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
curlybracket
Forum Commoner
Posts: 59
Joined: Mon Nov 29, 2010 2:40 pm

Re: Can you extract variable, from string of variables?

Post by curlybracket »

use print_r($array) to display array
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: Can you extract variable, from string of variables?

Post 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.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

Re: Can you extract variable, from string of variables?

Post 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.
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: Can you extract variable, from string of variables?

Post 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]
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

Re: Can you extract variable, from string of variables?

Post by McInfo »

:roll: The solution has been presented.
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: Can you extract variable, from string of variables?

Post 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.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

Re: Can you extract variable, from string of variables?

Post 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().

Code: Select all

parse_url($url, PHP_URL_QUERY)
  • $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.

Code: Select all

NULL;
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().

Code: Select all

print_r($vars);
  • 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",

Code: Select all

$vars['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.
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: Can you extract variable, from string of variables?

Post 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?).
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
Post Reply