Phil
Phil

Reputation: 14641

PHP iterating through a simple comma separated list

I have a string which can be

$string = "value.";

OR

$string = "value1, value2.";

I want to iterate through this string getting each item which are -> value (in first example) and -> value1 AND value2 in the second (without the comma or the dot in the end).

I was thinking of;

  1. Replace the dot in the end.
  2. Check if there is any comma.
  3. If there is comma, split-explode using ", " and iterate through.
  4. If not, only one item so just use it.

Is this the right way of doing it?

I am new to PHP and trying to learn best practices and best ways of solving the issues.

Thank you.

Upvotes: 7

Views: 42626

Answers (7)

Mosin
Mosin

Reputation: 608

try this code

$arr="a,b,c";
$variableAry=explode(",",$arr); 
foreach($variableAry as $var)
{
 echo"<input type='text' value=".$var." ><br/><br/>";
}

Upvotes: 1

sesser
sesser

Reputation: 1165

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.

Upvotes: 3

Sandeep Bansal
Sandeep Bansal

Reputation: 6394

$string = "value 1, value2";
$string = trim($string, ".");

$split = explode(",", $string);
print_r($split);

Upvotes: 2

Tyilo
Tyilo

Reputation: 30102

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
}

Upvotes: 30

anubhava
anubhava

Reputation: 784938

I would use following code:

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

OUTPUT

Array
(
    [0] => value1
    [1] => value2
)

Upvotes: 2

MetalFrog
MetalFrog

Reputation: 10513

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
//)

Upvotes: 1

Ignacio Vazquez-Abrams
Ignacio Vazquez-Abrams

Reputation: 798526

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

Upvotes: 1

Related Questions