3v4l.org

run code in 300+ PHP versions simultaneously
<?php function get_numerics ($str) { preg_match_all('/\d+/', $str, $matches); return $matches[0]; } $input = 'bubur 98k'; $inputNumbers = get_numerics($input); if (count($inputNumbers) !== 1) { // lebih dari 1 angka, atau angka null print_r("formatnya yang bener dong."); } else { $tempExpense = $inputNumbers[0]; // get index of first number occurence $pos = strpos($input, (string)$tempExpense); $k = ""; if (substr($input, $pos+1,1) === 'k') { // 3k $expense = $tempExpense * 1000; } elseif (substr($input, $pos+1,1) === '.') { // 100.300 } else { // 5000 $expense = $tempExpense; } print_r($tempExpense."k \n"); print_r($expense); }
Output for 7.0.0 - 7.0.24, 7.1.0 - 7.1.33, 7.2.0 - 7.2.33, 7.3.0 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.27, 8.2.0 - 8.2.17, 8.3.0 - 8.3.4
98k 98

preferences:
197.35 ms | 403 KiB | 218 Q