I want to explode a variable in a little different way

前端 未结 5 2011
遇见更好的自我
遇见更好的自我 2021-02-04 01:30

I have this variable.

$var = \"A,B,C,D,\'1,2,3,4,5,6\',E,F\";

I want to explode it so that I get the following array.

array(
[0         


        
5条回答
  •  感动是毒
    2021-02-04 01:56

    $var = "A,B,C,D,'1,2,3,4,5,6',E,F";
    
    $arr = preg_split("/(,)(?=(?:[^']|'[^']*')*$)/",$var);
    foreach ($arr as $data) {
        $requiredData[] = str_replace("'","",$data);
    }
    echo '
    ';
    print_r($requiredData);
    

    Description : Regular Exp. :-

    (?<=').*(?=') => Used to get all characters within single quotes(' '),
    
    |\w+ |(OR) => Used to get rest of characters excepted comma(,) 
    

    Then Within foreach loop i'm replacing single quote

提交回复
热议问题