PHP iterating through a simple comma separated list

后端 未结 7 2046
佛祖请我去吃肉
佛祖请我去吃肉 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:37

    You're absolutely right, you could do it like this:

    $string = 'foo, bar, baz.';
    $string = preg_replace('/\.$/', '', $string); //Remove dot at end if exists
    $array = explode(', ', $string); //split string into array seperated by ', '
    foreach($array as $value) //loop over values
    {
        echo $value . PHP_EOL; //print value
    }
    
    0 讨论(0)
提交回复
热议问题