想要将字符串转换为数组

时间:2016-12-19 10:49:48

标签: php codeigniter

我有一个这样的字符串 -

[answer] => 8|$0-$100,000<>9|$3-$100,000<>10|$2-$100,000.

我想把它变成这样的数组: -

[answer] => Array
    (
        [0] => 0-100,000
        [1] => 3-100,000
        [2] => 2-100,000
    )

3 个答案:

答案 0 :(得分:1)

试试这个;

$answer = "8|$0-$100,000<>9|$3-$100,000<>10|$2-$100,000";

$answer = preg_replace("^[0-9]*\|^", "", $answer); // remove the number and |
$answer = str_replace("$", "", $answer); // remove $ sign 
$answer = explode("<>", $answer);

答案 1 :(得分:0)

这应该可以胜任。

 $string = [answer];
 $string_array = explode("<>", $string);
 foreach($string_array as &$arr){
   $arr = substr($arr, 0, strpos('$'));
   $arr = str_replace('$', '', $arr);
 }

 var_dump($string_array);

答案 2 :(得分:0)

这应该可以胜任。

    $r = "8|$0-$100,000<>9|$3-$100,000<>10|$2-$100,000";
    $array = explode('<>', $r);
    foreach ($array as &$val) {
        $val = preg_replace("/^[0-9]+|/", "", $val);
        $val = str_replace(array('|', '$'), '', $val);
    }
    var_dump($array);