Reputation: 14641
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;
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
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
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
Reputation: 6394
$string = "value 1, value2";
$string = trim($string, ".");
$split = explode(",", $string);
print_r($split);
Upvotes: 2
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
Reputation: 784938
I would use following code:
$string = "value1, value2.";
$arr = explode(', ', trim($string, '.'));
print_r($arr);
Array
(
[0] => value1
[1] => value2
)
Upvotes: 2
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
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