Explode string based on special characters in php

I have a line:

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

      

+3


source to share


2 answers


This should work for you:

Just use preg_split()

with a class with all delimiters in it. At the end, just use array_shift()

to remove the first item.

<?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"
)

      

+3


source


You can use the function preg_split

with a regex pattern including all those separator characters. Then remove the first value of the array and reset:

$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" 
) 

      

0


source







All Articles