How to add comma after first 3 digits and after every 2 digits in PHP?

I’m trying to format some numbers in PHP to match the Bangladeshi taka format. Specifically, I want to add commas to the number after the first three digits and then after every two digits from right to left. I know how to use number_format() to add commas after every three digits, but I’m unsure how to modify it to achieve the desired format.

For example, if I have the number 10023456.789, I’d like to format it as 1,00,23,456.789.

Can anyone help me modify the code to achieve this format?

Thanks in advance for your help!

Here is a code you can try or modify as your need.

function format_currency( $num, $decimal_point = 2 ){

    // Make sure the number is decimal
    if ( !is_numeric($num) ) {
        return '0.00';

    $decimal_number = number_format($num, $decimal_point, '.', '');
    $num_array = explode('.', $decimal_number);

    if (count($num_array) != 2) {
        return '0.00';

    $decimal = $num_array[1];

    $numbers = strrev($num_array[0]);
    $pattern = [substr($numbers, 0, 3)];
    foreach (str_split(substr($numbers, 3), 2) as $part) {
        $pattern[] = $part;

    return strrev($decimal . '.' . implode(',', $pattern));

The function first checks if the input number is valid. If it’s not, the function returns ‘0.00’.

Then, the input number is formatted to the specified number of decimal points using the number_format() function. The formatted number is then split into an array using the dot (.) as the separator.

The function then extracts the decimal part of the number and store to $decimal variable.

The substr() function is used to extract the first three digits from the reversed number string, and then the str_split() function is used to split the remaining digits into groups of 2 digits each. These groups are then added to the array of patterns.

Then the array is joined with commas using implode() and concatenated with the decimal, and the resulting string is reversed before being returned.