7

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>
Script47
  • 14,230
  • 4
  • 45
  • 66
Saeed Heidarizarei
  • 8,406
  • 21
  • 60
  • 103

4 Answers4

4

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()">
Alex
  • 16,739
  • 1
  • 28
  • 51
  • 2
    @SedricHeidarizarei So what happened to "Thanks But I have Client side Solution, I need an example for server side"? You were advised to use Javascript almost 30 minutes ago and you refused. What changed? – Patrick Q Nov 12 '18 at 14:58
3

✅ 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

Saeed Heidarizarei
  • 8,406
  • 21
  • 60
  • 103
  • 1
    Odd, but interesting solution. Please note that users who cannot download this font will get arabic numbers displayed. – Lithilion Nov 16 '18 at 08:05
  • @Lithilion Yes that's right, It should also be replaced on Arabic too, I updated the answer with a new Photo. – Saeed Heidarizarei Nov 16 '18 at 11:52
  • 3
    No, thats not the point. If someone cannot load your custom font, there is a fallback to a system font (i.e. Arial, Times New Roman,...) and these will have the "standart" numbers on these places – Lithilion Nov 16 '18 at 11:54
  • Yes True But This Solution works 99% for all users if you use all font formats(eot.woff.woff2 and ...), – Saeed Heidarizarei Nov 16 '18 at 12:01
3

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.

Moradnejad
  • 3,466
  • 2
  • 30
  • 52
2

If you really want to do this in PHP, you can concat all text/html output into the same variable and then echo it in the end. Between the the last concat and the echo you can call this function, in every .php page you echo output.

<?php
//Code...
$string = ta_persian_num($string);
echo $string;

Personally I would prefer JS for this question.

Lithilion
  • 1,097
  • 2
  • 11
  • 26