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 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] 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] 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. Link to comment https://forums.phpfreaks.com/topic/14335-separating-words/#findComment-56494 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.