Jump to content

Take spaces out of comma seperated list


micah1701

Recommended Posts

the user submits a list of keywords, each seperated by a comma.
I want to alphabitize the list and take the spaces out between the keywords.

here's what I'm doing:[code]<?php
$postedList = $_POST['keywords']; // "Cranberry, Banana, Apple"
$keywords = explode(",",$postedList); //create array

foreach($keywords as $keyword){
$keyword = trim($keyword); //remove spaces from each value in array
}
sort($keywords);  //sort the array alphabetically
$data = implode(",",$keywords); //turn the array back into a list

echo $data; //returns:  " Apple, Banana,Cranberry" (with spaces) ???
?>[/code]

it still has spaces? What am I missing?
Link to comment
https://forums.phpfreaks.com/topic/15197-take-spaces-out-of-comma-seperated-list/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.