根据Php中的特殊字符来爆炸字符串

时间:2015-07-08 10:11:09

标签: php special-characters explode

我有一个字符串:

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

输出格式:

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

2 个答案:

答案 0 :(得分:4)

这应该适合你:

只需将preg_split()character class一起使用,其中包含所有分隔符。最后,只需使用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"
)

答案 1 :(得分:0)

您可以使用带有正则表达式的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" 
) 
相关问题