maybe try this:
$str = 'one,two,three,four';
$newstr = explode(',',
n = 'str'; //sufix
$c=1; //counter
foreach($newstr as $value){
n.$c; //assign vars
$
value; //assign value for var
$c++;
}
var_dump($str1);
var_dump($str2);
var_dump($str3);
var_dump($str4);
response:
string 'one' (length=3)
string 'two' (length=3)
string 'three' (length=5)
string 'four' (length=4)
Answer from miglio on Stack OverflowPhp Explode string and out put each string without showing array - Stack Overflow
If you explode a string and said string does not contain the ...
Explode PHP string by new line - Stack Overflow
Lightweight replacement for PHP explode function
Videos
maybe try this:
$str = 'one,two,three,four';
$newstr = explode(',',
n = 'str'; //sufix
$c=1; //counter
foreach($newstr as $value){
n.$c; //assign vars
$
value; //assign value for var
$c++;
}
var_dump($str1);
var_dump($str2);
var_dump($str3);
var_dump($str4);
response:
string 'one' (length=3)
string 'two' (length=3)
string 'three' (length=5)
string 'four' (length=4)
Use array!
<?php
$str = 'one,two,three,four';
$array= explode(',',$str);
//Print each array element
//('one' 'two' 'three' 'four' an so on...)
foreach($array as $element){
echo $element;
}
?>
Please read the php docs. It exists for a reason.
https://www.php.net/explode
Returns an array of strings created by splitting the string parameter on boundaries formed by the delimiter.
If delimiter is an empty string (""), explode() will return FALSE. If delimiter contains a value that is not contained in string and a negative limit is used, then an empty array will be returned, otherwise an array containing string will be returned.
No errors are thrown - it will return an array with one element which is the entire string (remember that explode returns an array).
Best Practice
As mentioned in the comment to the first answer, the best practice is to use the PHP constant PHP_EOL which represents the current system's EOL (End Of Line).
$skuList = explode(PHP_EOL, $_POST['skuList']);
PHP provides a lot of other very useful constants that you can use to make your code system independent, see this link to find useful and system independent directory constants.
Warning
These constants make your page system independent, but you might run into problems when moving from one system to another when you use the constants with data stored on another system. The new system's constants might be different from the previous system's and the stored data might not work anymore. So completely parse your data before storing it to remove any system dependent parts.
UPDATE
Andreas' comment made me realize that the 'Best Practice' solution I present here does not apply to the described use-case: the server's EOL (PHP) does not have anything to do with the EOL the browser (any OS) is using, but that (the browser) is where the string is coming from.
So please use the solution from @Alin_Purcaru to cover all your bases:
$skuList = preg_split('/\r\n|\r|\n/', $_POST['skuList']);
Cover all cases. Don't rely that your input is coming from a Windows environment.
$skuList = preg_split("/\\r\\n|\\r|\\n/", $_POST['skuList']);
or
$skuList = preg_split('/\r\n|\r|\n/', $_POST['skuList']);
In your code you are overwritting the $categories variable in each iteration. The correct code would look like:
$categories = '';
$cats = explode(",", $item['category_names']);
foreach($cats as $cat) {
$cat = trim($cat);
$categories .= "<category>" . $cat . "</category>\n";
}
update: as @Nanne suggested, explode only on ','
Without a for loop
$item['category_names'] = "Hair, Fashion, News";
$categories = "<category>".
implode("</category>\n<category>",
array_map('trim', explode(",", $item['category_names']))) .
"</category>\n";
echo $categories;