PHP Explode, Trim and Drop Empties
Often after you explode a string, you need to trim the values and discard empty keys.
Here is a simple function to do all that in one step:
/**
* Explode string, trim, and drop empty keys
*
* @param (string) $string - boundary string
* @param (string) $delimiter - input string
*
* @return (array)
**/
function explodeClean( $delimiter, $string )
{
if( !is_string( $delimiter ) || !is_string( $string ) ) return $string;
$string = explode( $delimiter, $string );
foreach( $string as $k => $v )
{
$v = trim( $v );
if( $v ) $string[$k] = $v; else unset( $string[$k] );
}
return $string;
}
Top