I want to explode a variable in a little different way

前端 未结 5 1935
遇见更好的自我
遇见更好的自我 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:49

    Simply use preg_match_all with the following regex as follows

    preg_match_all("/(?<=').*(?=')|\w+/",$var,$m);
    print_r($m[0]);
    

    Regex Explanation :

    • (?<=').*(?=') Capture each and every character within '(quotes)
    • |\w+ |(OR) Will grab rest of the characters except ,

    Demo

    Regex

    0 讨论(0)
  • 2021-02-04 01:54

    Although preg_split along with array_map is working very good, see below an example using explode and trim

    $var = "A,B,C,D,'1,2,3,4,5,6',E,F";
    
    $a = explode("'",$var);
    
    //print_r($a);
    /*
    outputs
    Array
    (
        [0] => A,B,C,D,
        [1] => 1,2,3,4,5,6
        [2] => ,E,F
    )
    */
    $firstPart = explode(',',trim($a[0],',')); //take out the trailing comma
    /*
    print_r($firstPart);
    outputs
    Array
    (
        [0] => A
        [1] => B
        [2] => C
        [3] => D
    )
    */
    $secondPart = array($a[1]);
    
    $thirdPart = explode(',',trim($a[2],',')); //tale out the leading comma
    /*
    print_r($thirdPart);
    Array
    (
        [0] => E
        [1] => F
    )
    */
    
    $fullArray = array_merge($firstPart,$secondPart,$thirdPart);
    print_r($fullArray);
    /*
    ouputs
    Array
    (
        [0] => A
        [1] => B
        [2] => C
        [3] => D
        [4] => 1,2,3,4,5,6
        [5] => E
        [6] => F
    )
    */
    
    0 讨论(0)
  • 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 '<pre>';
    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

    0 讨论(0)
  • 2021-02-04 02:04

    You need to explode the string to array.

    But, you need commas after every element except last one.

    Here is working example:

    <?php
    $var = "A,B,C,D,'1,2,3,4,5,6',E,F";
    $arr = explode("'", $var);
    $num = ! empty($arr[1]) ? str_replace(',', '_', $arr[1]) : '';
    $nt = $arr[0] . $num . $arr[2]; 
    $nt = explode(',', $nt);
    $len = count($nt);
    $na = array();
    $cnt = 0;
    foreach ($nt as $v) {
        $v = str_replace('_', ',', $v);
        $v .= ($cnt != $len - 1) ? ',' : '';
        $na[] = $v;
        ++$cnt; 
    }
    

    Demo

    0 讨论(0)
  • 2021-02-04 02:07

    There is an existing function that can parse your comma-separated string. The function is str_getcsv

    It's signature is like so:

    array str_getcsv ( string $input [, string $delimiter = "," [, string $enclosure = '"' [, string $escape = "\\" ]]] )
    

    Your only change would be to change the 3rd variable, the enclosure, to single quotes rather than the default double quotes.

    Here is a sample.

    $var = "A,B,C,D,'1,2,3,4,5,6',E,F";
    $array = str_getcsv($var,',',"'");
    

    If you var_dump the array, you'll get the format you wanted:

    array(7) {
      [0]=>
      string(1) "A"
      [1]=>
      string(1) "B"
      [2]=>
      string(1) "C"
      [3]=>
      string(1) "D"
      [4]=>
      string(11) "1,2,3,4,5,6"
      [5]=>
      string(1) "E"
      [6]=>
      string(1) "F"
    }
    
    0 讨论(0)
提交回复
热议问题