- is_numeric: documentation ( source)
<?php
$str = "1 -4 5 6 -2";
$e = "";
$m = "-";
for($i = 0, $len = strlen($str); $i < $len; $i++) {
if($m == $str[$i]) {
$e = $m;
}
else {
$e = "";
}
if(is_numeric($str[$i])) {
$result[] = $e . $str[$i];
}
}
echo $result;