- substr: documentation ( source)
- strpos: documentation ( source)
- print_r: documentation ( source)
- preg_match_all: documentation ( source)
<?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);
}