When I split a string with spaces it's not removing the sapces. My string looks like this:
$string = 'one - two - three';
My PHP call looks like this:
list($blk1, $blk2, $blk3) = split('\s*\-\s*', $string);
I would assume the regex will split on zero or more spaces but the spaces remain in the resulting array.
Any idea why?
You could try something like:
[\s-]+
Not 100% familiar with php but it should work.
I don't know for sure what to tell you on the direct question but as you can see on the man page http://php.net/split this function is deprecated, you shouldn't rely on it.
Why don't you just use explode like this:
$string = 'one - two - three';
$parts = explode(' - ', $string);
$blk1 = $parts[0];
$blk2 = $parts[1];
$blk3 = $parts[2];
split
is long ago deprecated in PHP. Consider using `preg_split'
$string = 'one - two - three';
list($blk1, $blk2, $blk3) = preg_split('/\s*\-\s*/', $string);
Why not using explode() ?
function trim_value(&$value)
{
$value = trim($value);
}
$array = explode("-", $string);
array_walk($array, 'trim_value');
There you go: http://codepad.viper-7.com/ULr1NO
Just as a counter against regular expressions for such trivial task with a delimited string:
$string = 'one - two - three';
$parts = explode(' - ', $string);
array_walk($parts, 'trim');
var_dump($parts);