PHP iterating through a simple comma separated list

后端 未结 7 2045
佛祖请我去吃肉
佛祖请我去吃肉 2021-01-01 03:06

I have a string which can be

$string = \"value.\";

OR

$string = \"value1, value2.\";

I want to iterate th

相关标签:
7条回答
  • 2021-01-01 03:12

    I would use following code:

    $string = "value1, value2.";
    $arr = explode(', ', trim($string, '.'));
    print_r($arr);
    

    OUTPUT

    Array
    (
        [0] => value1
        [1] => value2
    )
    
    0 讨论(0)
  • 2021-01-01 03:12

    try this code

    $arr="a,b,c";
    $variableAry=explode(",",$arr); 
    foreach($variableAry as $var)
    {
     echo"<input type='text' value=".$var." ><br/><br/>";
    }
    
    0 讨论(0)
  • 2021-01-01 03:17

    I know this has been answered, but, you could do this with one command:

    $input = "foo, bar, baz., shibby, poop.";
    
    //-- handles it all in one pass
    $output = preg_split('/[\.,\s]/', $input, -1, PREG_SPLIT_NO_EMPTY);
    
    //-- just output
    array_walk($output, function(&$item, $idx) {
        echo $idx . ': ' . $item . PHP_EOL;
    });
    

    If you're expecting spaces in your values, however, this will split them up too. I just added \s to the regex to trim the elements. If you expect spaces in values, then omit the \s in the regex and trim() the elements later.

    Or, this regex will produce the same: /(\.\s?|,\s?)/

    $input = "foo, bar, baz., shibby, poop. foo bar";
    
    //-- handles it all in one pass
    $output = preg_split('/(\.\s?|,\s?)/', $input, -1, PREG_SPLIT_NO_EMPTY);
    
    //-- just output
    array_walk($output, function(&$item, $idx) {
        echo $idx . ': ' . $item . PHP_EOL;
    });
    

    Produces

    0: foo
    1: bar
    2: baz
    3: shibby
    4: poop
    5: foo bar
    

    The Regex: explained

    The original regex simply split on single characters in the string as denoted by the [ and ]. Thus, /[\.,\s]/ matches on period, comma, AND space characters in the string. After thinking about it, this is probably not what you want but close.

    The updated regex is a little different and splits on two different scenarios. \.\s? says: split on the period and/or preiod+space. The ? makes the preceding optional (I believe 0 or 1). Same with the ,\s?. Match on comma and/or comma+space. This accounts for all the scenarios. Using grouping ((...)) instead of character ranges allows for this. The pipe | in the middle means OR so, the complete regex means: match on period and/or period+space OR comma and/or comma+space.

    0 讨论(0)
  • 2021-01-01 03:22

    You should not do 2 and 4. If there is no comma then it will split to an array of one element.

    0 讨论(0)
  • 2021-01-01 03:29

    When you explode, and there is no delimiter you have requested, the result at index 0 is the entire string.

    <?php
    $string = "test, test2., test3, test4";
    print_r( explode(',', $string) );
    //Array
    //(
    //    [0] => test
    //    [1] =>  test2.
    //    [2] =>  test3
    //    [3] =>  test4
    //)
    
    $string = "test test2. test3 test4";
    print_r( explode(',', $string) );
    //Array
    //(
    //    [0] => test test2. test3 test4
    //)
    
    0 讨论(0)
  • 2021-01-01 03:31
    $string = "value 1, value2";
    $string = trim($string, ".");
    
    $split = explode(",", $string);
    print_r($split);
    
    0 讨论(0)
提交回复
热议问题