tags:

views:

53

answers:

3

I have this string:

type=openBook&bookid=&guid=7AD92237-D3C7-CD3E-C052-019E1EBC16B8&authorid=&uclass=2&view=

Then I want to get all the values after the "=" sign so for the "type" i want to get "openBook" and put this in an array.

Note: even if it is null it must be added to the array so i wont loose track..

So how can i do that.

Many Thanks

+7  A: 

I think you want parse_str:

<?php
$str = "first=value&arr[]=foo+bar&arr[]=baz";
parse_str($str);
echo $first;  // value
echo $arr[0]; // foo bar
echo $arr[1]; // baz

parse_str($str, $output);
echo $output['first'];  // value
echo $output['arr'][0]; // foo bar
echo $output['arr'][1]; // baz
?>

I'm not sure what you mean by this bit:

Note: even if it is null it must be added to the array so i wont loose track..

If parse_str doesn't work quite as you want post a comment and I'll try and help.

Dominic Rodger
+1. I think he wanted to have `type` included in the array even when it is not defined (or empty?) in the string.
Franz
ow sorry about that what i meant was. if type="" it must also store a null or empty value to the array
Treby
Thanks Bro, I didn't need an array cause it is in a variable already..
Treby
A: 
$first = split("&", "type=openBook&bookid=&guid=7AD92237-D3C7-CD3E-C052-019E1EBC16B8&authorid=&uclass=2&view=");

foreach($first as $value) {
    list($key, $val) = split("=", $value);
    $arr[$key] = $val;
}

echo $arr['type'];    // openBook
Ei Maung
explode() would be better than split(). First, you don't use regular expression, second - split is deprecated since 5.3
Vafliik
+1  A: 

You can take a look to explode();

<?php

$foo = "type=openBook&bookid=&guid=7AD92237-D3C7-CD3E-C052-019E1EBC16B8&authorid=&uclass=2&view=";

$chuncks = explode('&', $foo);

$data = array();

foreach ($chuncks as $chunck)
{
  $bar = explode('=', $chunck);
  $data[$bar[0]] = $bar[1];

}

var_dump($data);
Boris Guéry