nick5449 Posted July 11, 2006 Share Posted July 11, 2006 I have a group of words that are separated by a comma. Such as "blah, blahblah blah". I want to separate everything before and after the comma into two separate variables and also without the comma in there. Can someone point me in the right direction? Thanks for any help Quote Link to comment https://forums.phpfreaks.com/topic/14335-separating-words/ Share on other sites More sharing options...
ShogunWarrior Posted July 11, 2006 Share Posted July 11, 2006 [code]function strip_blanks($arr){ foreach( $arr as $ki=>$val ) { if(trim($val)=='') {unset($arr[$ki]);} else{$arr[$ki] = trim($val);} }return($arr);}//Replace String$str = "Hello, this is a comma, separated, string with, comas!!";$expl = explode(',',$str);$stripped = strip_blanks($expl);[/code]Stripped will now contain:[code]Array( [0] => Hello [1] => this is a comma [2] => separated [3] => string with [4] => comas!!)[/code] Quote Link to comment https://forums.phpfreaks.com/topic/14335-separating-words/#findComment-56488 Share on other sites More sharing options...
effigy Posted July 11, 2006 Share Posted July 11, 2006 A little more concise, and it removes surrounding whitespace:[code]<?php $str = "Hello, this is a comma, separated, string with, comas!!"; $array = preg_split('/\s*,\s*/', $str); echo '<pre>', print_r($array, true), '</pre>';?>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/14335-separating-words/#findComment-56492 Share on other sites More sharing options...
ShogunWarrior Posted July 11, 2006 Share Posted July 11, 2006 Nice one. Quote Link to comment https://forums.phpfreaks.com/topic/14335-separating-words/#findComment-56494 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.