-1

My string is like the following format:

$string = "name=xxx&id=11&name=yyy&id=12&name=zzz&id=13&name=aaa&id=10";

I want to split the string like the following:

$str[0] = "name=xxx&id=11";
$str[1] = "name=yyy&id=12";
$str[2] = "name=zzz&id=13";
$str[3] = "name=aaa&id=10";

How can I do this in PHP?

2
  • 3
    Me thinks you're doing something wrong. Where'd you get that string from? Commented Jun 21, 2010 at 6:39
  • 2
    If you didn't have duplicate keys (name, id), you could use parse_str() Commented Jun 21, 2010 at 7:06

5 Answers 5

8

Try this:

$matches = array();
preg_match_all("/(name=[a-zA-Z0-9%_-]+&id=[0-9]+)/",$string,$matches);

$matches is now an array with the strings you wanted.

Update

function get_keys_and_values($string /* i.e. name=yyy&id=10 */) {
  $return = array();
  $key_values = split("&",$string);
  foreach ($key_values as $key_value) {
    $kv_split = split("=",$key_value);
    $return[$kv_split[0]] = urldecode($kv_split[1]);
  }
  return $return;
}
Sign up to request clarification or add additional context in comments.

Comments

3
$string = "name=xxx&id=11&name=yyy&id=12&name=zzz&id=13&name=aaa&id=10";
$arr = split("name=", $string);

$strings = aray();
for($i = 1; $i < count($arr), $i++){
    $strings[$i-1] = "name=".substr($arr[$i],0,-1);
}

The results will be in $strings

Comments

3

I will suggest using much simpler term

Here is an example

$string = "name=xxx&id=11;name=yyy&id=12;name=zzz&id=13;name=aaa&id=10";
$arr = explode(";",$string); //here is your array

3 Comments

won't work if he wants to split parameters of an already defined url
@Thariama, then OP would ask regarding URL not string
depends. he then should have asked for an url, but some users mean "url" when they say "string"
0

Any surgery conducted on this malformed querystring may potentially corrupt data.

If you know that the input will not have = in the key name or values, then you can correct the querystring by injecting [] between each key name and the equals sign, then parse it normally and transpose if you wish.

Code: (Demo)

parse_str(str_replace('=', '[]=', $string), $array);
var_export(array_map(fn($name, $id) => get_defined_vars(), $array['name'], $array['id']));

Output:

array (
  0 => 
  array (
    'name' => 'xxx',
    'id' => '11',
  ),
  1 => 
  array (
    'name' => 'yyy',
    'id' => '12',
  ),
  2 => 
  array (
    'name' => 'zzz',
    'id' => '13',
  ),
  3 => 
  array (
    'name' => 'aaa',
    'id' => '10',
  ),
)

If this isn't strict enough, you can tighten the replacement process to explicitly nominate name and id keys.

parse_str(preg_replace('/\b(name|id)\K(?==)/', '[]', $string), $array);

Ideally, if you have control of the form fields which are producing this submission payload, you should most simply add [] the the fields' name attributes (<input type="text" name="name[]"> and <input type="text" name="id[]">) or otherwise correct the process that is creating key collisions in the payload.

Comments

-1

If you want to do what you asked, nothing more or less , that's explode('&', $string).

If you have botched up your example and you have a HTTP query string then you want to look at parse_str().

1 Comment

this won't work because of duplicate strings like name and id

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.