Jump to content


Photo

Take spaces out of comma seperated list


  • Please log in to reply
9 replies to this topic

#1 micah1701

micah1701
  • Members
  • PipPipPip
  • Advanced Member
  • 613 posts
  • LocationEllington, CT USA

Posted 20 July 2006 - 08:48 PM

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:
<?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) ??? 
?>

it still has spaces? What am I missing?
"Confidence in the face of risk."

#2 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 20 July 2006 - 08:49 PM

trim only removes spaces from the start and end of a string. it will not remove spaces between characters. If you want to remove the spaces use str_replace

#3 effigy

effigy
  • Staff Alumni
  • Advanced Member
  • 3,600 posts
  • LocationIL

Posted 20 July 2006 - 08:51 PM

$keywords = preg_split('/\s*,\s*/', $postedList);

Regexp | Unicode Article | Letter Database
/\A(e)?((1)?ff(?:(?:ig)?y)?|f(?:ig)?)\z/

#4 ryanlwh

ryanlwh
  • Staff Alumni
  • Advanced Member
  • 511 posts

Posted 20 July 2006 - 08:53 PM

You didn't put the trimmed keywords back into the array

foreach($keywords as $index=>$keyword){ 
$keyword = trim($keyword); //remove spaces from each value in array
$keywords[$index] = $keyword;
}

Edit: effigy has the better solution.
Please use EDIT * 100...
Please use
or [php] * 1000...

PLEASE READ THE POSTED SOLUTIONS CAREFULLY * 1000000...

#5 effigy

effigy
  • Staff Alumni
  • Advanced Member
  • 3,600 posts
  • LocationIL

Posted 20 July 2006 - 08:54 PM

Actually, although the preg_split is better, you needed to reference $keyword:

foreach ($keywords as &$keyword) { ... }

Regexp | Unicode Article | Letter Database
/\A(e)?((1)?ff(?:(?:ig)?y)?|f(?:ig)?)\z/

#6 micah1701

micah1701
  • Members
  • PipPipPip
  • Advanced Member
  • 613 posts
  • LocationEllington, CT USA

Posted 20 July 2006 - 08:59 PM

thanks effigy for both your posts.
that solves my problem.
I guess I didn't grasp the concept as well as I had thought.
"Confidence in the face of risk."

#7 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 20 July 2006 - 09:31 PM

Use the function array_map()  http://www.php.net/m...n.array-map.php
<?php $keywords = array_map(trim,$keywords); ?>

Nice and simple.

Ken

#8 micah1701

micah1701
  • Members
  • PipPipPip
  • Advanced Member
  • 613 posts
  • LocationEllington, CT USA

Posted 20 July 2006 - 11:27 PM

kenrbnsn, I like that. I've never noticed that array function before.
karma++
"Confidence in the face of risk."

#9 akitchin

akitchin
  • Staff Alumni
  • Advanced Member
  • 2,516 posts
  • LocationCalgary, AB, Canada

Posted 20 July 2006 - 11:30 PM

it should be noted that array_map() will only work for one level of an array.  check the comments for a deep_map() type of function that will map the function to all levels of an array.

#10 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 20 July 2006 - 11:33 PM

I didn't know it existed until about a month ago when it was mentioned as a reply to a reply I made to another question here.

Now, I seem to be using it all over. :)

Ken




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users