Skip to content

Instantly share code, notes, and snippets.

@kjantzer
Last active August 29, 2015 14:11
Show Gist options
  • Select an option

  • Save kjantzer/3da1d9ce55a658ee4389 to your computer and use it in GitHub Desktop.

Select an option

Save kjantzer/3da1d9ce55a658ee4389 to your computer and use it in GitHub Desktop.
SmartSplit - requires underscore.js
function smart_split($str){
$vals = array();
if( preg_match("/,/", $str) ) // comma delimited
$vals = explode(',', $str);
else if( preg_match("/\t/", $str) )
$vals = explode("\t", $str);
else
$vals = explode("\n", $str);
foreach($vals as &$val){ $val = trim($val); }
return $vals;
}
smartSplit = function(str){
var vals = [];
if( str.match(/,/) ) // comma separated
vals = str.split(',')
else if( str.match(/\t/) ) // tab separated
vals = str.split("\t")
else
vals = str.split("\n") // new line separated
return _.invoke(vals, 'trim') // trim whitespace
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment