3v4l.org

run code in 300+ PHP versions simultaneously
<?php var_dump(stringToEnglishNumber('-214-277. 4404-', ',')); function stringToEnglishNumber($string, $separator = '') { $sanitized = preg_replace("/[^\-0-9,. ]/", '', $string); $split = str_split($sanitized); if (empty($split)) { return $string; } if ($split[0] == '-') { $split[0] = 'negative'; } $convert = [ '-' => 'dash', '.' => 'dot', ' ' => 'space', ]; foreach ($split as $key => $char) { if (array_key_exists($char, $convert)) { $split[$key] = $convert[$char]; } } return implode(" {$separator}", $split); }
Output for 5.4.0 - 5.4.45, 5.5.0 - 5.5.38, 5.6.0 - 5.6.30, 7.0.0 - 7.0.20, 7.1.0 - 7.1.10, 7.2.0 - 7.2.33, 7.3.16 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.28, 8.2.0 - 8.2.18, 8.3.0 - 8.3.6
string(62) "negative ,2 ,1 ,4 ,dash ,2 ,7 ,7 ,dot ,space ,4 ,4 ,0 ,4 ,dash"

preferences:
228.48 ms | 404 KiB | 285 Q