0

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

3 Answers 3

7

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.

Sign up to request clarification or add additional context in comments.

2 Comments

+1. I think he wanted to have type included in the array even when it is not defined (or empty?) in the string.
ow sorry about that what i meant was. if type="" it must also store a null or empty value to the array
1

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);

Comments

-1
$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

1 Comment

explode() would be better than split(). First, you don't use regular expression, second - split is deprecated since 5.3

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.