John Davidson

php - Explode String by Comma and again with space

0 comments
Message:


I have a string of numbers, like so:


$numbers = "one, two, three four, five";

I first explode it by comma:


$numbers_array = explode(",", $numbers);

My result is:


array(4) {
[0] => string(3) "one"
[1] => string(4) " two"
[2] => string(11) " three four"
[3] => string(5) " five"
}

My question is, how can I achieve this?


array(5) { 
[0] => string(3) "one"
[1] => string(4) "two"
[2] => string(6) "three"
[3] => string(5) "four"
[4] => string(5) "five"
}


My Answer:
"1,2,3,4,5,6,7,8,9,10";

// Explode the string by comma
$numbers_array = explode(",", $numbers);

// Implode the array with space
$numbers_string = implode(" ", $numbers_array);

echo $numbers_string; // Output: 1 2 3 4 5 6 7 8 9 10

Rate this post

4 of 5 based on 1028 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us