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>
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()">
✅ 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:
Update:
It should also be replaced on Arabic too
Final View Photo
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With