Show 1k instead of 1,000

Jake picture Jake · Dec 18, 2011 · Viewed 10.5k times · Source
    function restyle_text($input){
    $input = number_format($input);
    $input_count = substr_count($input, ',');
    if($input_count != '0'){
        if($input_count == '1'){
            return substr($input, +4).'k';
        } else if($input_count == '2'){
            return substr($input, +8).'mil';
        } else if($input_count == '3'){
            return substr($input, +12).'bil';
        } else {
            return;
        }
    } else {
        return $input;
    }
}

This is the code I have, I thought it was working. apparently not.. can someone help since I can't figure this out.

Answer

Indranil picture Indranil · Dec 18, 2011

Try this:

http://codepad.viper-7.com/jfa3uK

function restyle_text($input){
    $input = number_format($input);
    $input_count = substr_count($input, ',');
    if($input_count != '0'){
        if($input_count == '1'){
            return substr($input, 0, -4).'k';
        } else if($input_count == '2'){
            return substr($input, 0, -8).'mil';
        } else if($input_count == '3'){
            return substr($input, 0,  -12).'bil';
        } else {
            return;
        }
    } else {
        return $input;
    }
}

Basically, I think you're using the substr() wrong.