yii\helpers\BaseStringHelper::explode PHP Method

explode() public static method

Explodes string into array, optionally trims values and skips empty ones
Since: 2.0.4
public static explode ( string $string, string $delimiter = ',', mixed $trim = true, boolean $skipEmpty = false ) : array
$string string String to be exploded.
$delimiter string Delimiter. Default is ','.
$trim mixed Whether to trim each element. Can be: - boolean - to trim normally; - string - custom characters to trim. Will be passed as a second argument to `trim()` function. - callable - will be called for each value instead of trim. Takes the only argument - value.
$skipEmpty boolean Whether to skip empty strings between delimiters. Default is false.
return array
    public static function explode($string, $delimiter = ',', $trim = true, $skipEmpty = false)
    {
        $result = explode($delimiter, $string);
        if ($trim) {
            if ($trim === true) {
                $trim = 'trim';
            } elseif (!is_callable($trim)) {
                $trim = function ($v) use($trim) {
                    return trim($v, $trim);
                };
            }
            $result = array_map($trim, $result);
        }
        if ($skipEmpty) {
            // Wrapped with array_values to make array keys sequential after empty values removing
            $result = array_values(array_filter($result, function ($value) {
                return $value !== '';
            }));
        }
        return $result;
    }

Usage Example

Beispiel #1
0
 /**
  * Overrides parent method with $skipEmpty default value set to true.
  * @inheritdoc
  */
 public static function explode($string, $delimiter = ',', $trim = true, $skipEmpty = true)
 {
     return parent::explode($string, $delimiter, $trim, $skipEmpty);
 }