4

I have a string:

xyz.com?username="test"&pwd="test"@score="score"#key="1234"

output format:

array (
    [0] => username="test"
    [1] => pwd="test"
    [2] => score="score"
    [3] => key="1234"
)
Rizier123
  • 58,877
  • 16
  • 101
  • 156
sac
  • 137
  • 2
  • 5
  • 15

2 Answers2

5

This should work for you:

Just use preg_split() with a character class with all delimiters in it. At the end just use array_shift() to remove the first element.

<?php

    $str = 'xyz.com?username="test"&pwd="test"@score="score"#key="1234"';

    $arr = preg_split("/[?&@#]/", $str);
    array_shift($arr);

    print_r($arr);

?>

output:

Array
(
    [0] => username="test"
    [1] => pwd="test"
    [2] => score="score"
    [3] => key="1234"
)
Rizier123
  • 58,877
  • 16
  • 101
  • 156
1

You can use preg_split function with regex pattern including all those delimimting special characters. Then remove the first value of the array and reset keys:

$s = 'xyz.com?username="test"&pwd="test"@score="score"#key="1234"';
$a = preg_split('/[?&@#]/',$s);
unset($a[0]);
$a = array_values($a);

print_r($a);

Output:

Array ( 
[0] => username="test" 
[1] => pwd="test" 
[2] => score="score" 
[3] => key="1234" 
) 
n-dru
  • 9,285
  • 2
  • 29
  • 42