根据Php中的特殊字符分解字符串

To explode the string based on the special characters in Php

我有一个字符串:

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

输出格式:

array (
    [0] => username="test"
    [1] => pwd="test"
    [2] => score="score"
    [3] => key="1234"
)

您可以将 preg_split 函数与正则表达式模式一起使用,包括所有那些分隔特殊字符。然后删除数组的第一个值并重置键:

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

输出:

Array ( 
[0] => username="test" 
[1] => pwd="test" 
[2] => score="score" 
[3] => key="1234" 
) 

这应该适合你:

只需使用 preg_split() with a character class with all delimiters in it. At the end just use array_shift() 删除第一个元素。

<?php

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

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

    print_r($arr);

?>

输出:

Array
(
    [0] => username="test"
    [1] => pwd="test"
    [2] => score="score"
    [3] => key="1234"
)