Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to replace entire numbers in a website to persian numbers via PHP?

Tags:

php

How to replace entire numbers in body or website html to persian numbers via PHP?
I want to replace all numbers in my website for all pages .

Code:

function ta_persian_num($string) {
  //arrays of persian and latin numbers
  $persian_num = array('۰', '۱', '۲', '۳', '۴', '۵', '۶', '۷', '۸', '۹');
  $latin_num = range(0, 9);

  $string = str_replace($latin_num, $persian_num, $string);

  return $string;
}

My Code work for Client Side:

<script>
    $(document).ready(function(){
    persian={0:'۰',1:'۱',2:'۲',3:'۳',4:'۴',5:'۵',6:'۶',7:'۷',8:'۸',9:'۹'};
    function traverse(el){
        if(el.nodeType==3){
            var list=el.data.match(/[0-9]/g);
            if(list!=null && list.length!=0){
                for(var i=0;i<list.length;i++)
                    el.data=el.data.replace(list[i],persian[list[i]]);
            }
        }
        for(var i=0;i<el.childNodes.length;i++){
            traverse(el.childNodes[i]);
        }
    }
    traverse(document.body);
});
</script>
like image 745
Saeed Heidarizarei Avatar asked Nov 12 '18 14:11

Saeed Heidarizarei


Video Answer


3 Answers

You should better move this functionality to frontend end use javascript:

<script type="text/javascript">
var replaceDigits = function() {
    var map = ["&\#1776;","&\#1777;","&\#1778;","&\#1779;","&\#1780;", "&\#1781;","&\#1782;","&\#1783;","&\#1784;","&\#1785;"]
    document.body.innerHTML = document.body.innerHTML.replace(/\d(?=[^<>]*(<|$))/g, function($0) { return map[$0]});
}
window.onload = replaceDigits;
</script>

Ex: https://codepen.io/anon/pen/rQWGRd

or you can include this function in any your js file which is loaded on all pages.

And also your body tag should have a onload attribute like the following:

<body onload="replaceDigits()">
like image 197
Alex Avatar answered Oct 19 '22 21:10

Alex


✅ Solved My Problem with This way:

1: Use FontCreator 9 or 10 Program.
2: Open your font in FontCreator.
3: Goto Numbers tab.
4: Paste your RTL language numbers (Persian/Hebrew/Arabic) On english numbers.

Enjoy Without any F... Functions / Extra Processing.

Photo: Photo

Update:
It should also be replaced on Arabic too

Final View Photo Photo2

like image 3
Saeed Heidarizarei Avatar answered Oct 19 '22 23:10

Saeed Heidarizarei


The nicest way to do in PHP, is using regular expressions:

$string='salam 1 23 12';
$persian_num = array('۰', '۱', '۲', '۳', '۴', '۵', '۶', '۷', '۸', '۹');
for($i=0;$i<10;$i++){
    $string = preg_replace("/$i/", $persian_num[$i], $string);
}
print($string);

Code above, replaces all English numbers with your specified characters in Persian.

like image 3
Moradnejad Avatar answered Oct 19 '22 22:10

Moradnejad