micah1701 Posted July 20, 2006 Share Posted July 20, 2006 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 arrayforeach($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 listecho $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 More sharing options...
wildteen88 Posted July 20, 2006 Share Posted July 20, 2006 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 Link to comment https://forums.phpfreaks.com/topic/15197-take-spaces-out-of-comma-seperated-list/#findComment-61328 Share on other sites More sharing options...
effigy Posted July 20, 2006 Share Posted July 20, 2006 [code]$keywords = preg_split('/\s*,\s*/', $postedList);[/code] Link to comment https://forums.phpfreaks.com/topic/15197-take-spaces-out-of-comma-seperated-list/#findComment-61330 Share on other sites More sharing options...
ryanlwh Posted July 20, 2006 Share Posted July 20, 2006 You didn't put the trimmed keywords back into the array[code]foreach($keywords as $index=>$keyword){ $keyword = trim($keyword); //remove spaces from each value in array$keywords[$index] = $keyword;}[/code]Edit: effigy has the better solution. Link to comment https://forums.phpfreaks.com/topic/15197-take-spaces-out-of-comma-seperated-list/#findComment-61331 Share on other sites More sharing options...
effigy Posted July 20, 2006 Share Posted July 20, 2006 Actually, although the preg_split is better, you needed to reference $keyword:[code]foreach ($keywords as &$keyword) { ... }[/code] Link to comment https://forums.phpfreaks.com/topic/15197-take-spaces-out-of-comma-seperated-list/#findComment-61334 Share on other sites More sharing options...
micah1701 Posted July 20, 2006 Author Share Posted July 20, 2006 thanks effigy for both your posts.that solves my problem. I guess I didn't grasp the concept as well as I had thought. Link to comment https://forums.phpfreaks.com/topic/15197-take-spaces-out-of-comma-seperated-list/#findComment-61337 Share on other sites More sharing options...
kenrbnsn Posted July 20, 2006 Share Posted July 20, 2006 Use the function array_map() http://www.php.net/manual/en/function.array-map.php[code]<?php $keywords = array_map(trim,$keywords); ?>[/code]Nice and simple.Ken Link to comment https://forums.phpfreaks.com/topic/15197-take-spaces-out-of-comma-seperated-list/#findComment-61352 Share on other sites More sharing options...
micah1701 Posted July 20, 2006 Author Share Posted July 20, 2006 kenrbnsn, I like that. I've never noticed that array function before.karma++ Link to comment https://forums.phpfreaks.com/topic/15197-take-spaces-out-of-comma-seperated-list/#findComment-61425 Share on other sites More sharing options...
akitchin Posted July 20, 2006 Share Posted July 20, 2006 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. Link to comment https://forums.phpfreaks.com/topic/15197-take-spaces-out-of-comma-seperated-list/#findComment-61428 Share on other sites More sharing options...
kenrbnsn Posted July 20, 2006 Share Posted July 20, 2006 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 Link to comment https://forums.phpfreaks.com/topic/15197-take-spaces-out-of-comma-seperated-list/#findComment-61433 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.