Blade Traverse for farsi numbers

Submitted by bigsinoos - 10 years ago

blade extension that displays numbers in Farsi instead of Latin ( "۵" instead of "5").

/**
* after putting this simple blade extension
* in globals.php or anywhere else
* you can use it in blade views like @traverse_farsi($number)
*/
Blade::extend(function($view, $compiler)
{
    $pat = $compiler->createMatcher('traverse_farsi');
    return preg_replace($pat, '$1<?php echo traverse_farsi($2) ;?>', $view);
});

function traverse_farsi ($str){
    $farsi_chars = ['٠','١','٢','٣','٤','٥','٦','٧','٨','٩'];
    $latin_chars = ['0','1','2','3','4','5','6','7','8','9'];
    return str_replace($latin_chars,$farsi_chars,$str);
}