我想將英文數字(0,1,2,3,...)轉換爲阿拉伯數字(0,1,2,3,...)一些使用PHP函數的HTML文檔。
我寫了這個功能:將英文數字轉換爲html文檔中的阿拉伯數字
function en2ar($str) {
$ends = array('0','1','2','3','4','5','6','7','8','9');
$ards = array('۰','۱','۲','۳','۴','۵','۶','۷','۸','۹');
return str_replace($ends,$ards,$str);
}
,但所有的數字轉換文件,而我只想數字轉換成的文檔內容。
例如,我想轉換:
<h1 style="color: #333;">1</h1>
<div style="width: 180px;">2</div>
到:
<h1 style="color: #333;">۱</h1>
<div style="width: 180px;">۲</div>
,但其轉換爲:
<h۱ style="color: #۳۳۳;">۱</h۱>
<div style="width: ۱۸۰px;">۲</div>
,使文件無效。
使用[XML解析器來處理DOM ](http://www.php.net/manual/en/class.domdocument.php),然後[找到文本節點](http://stackoverflow.com/a/76 8800/2908724)並根據需要進行更新。 – bishop
題外話:哇!我認爲阿拉伯數字是1,2,3 ... – Tivie
使用正則表達式不是更好嗎? – Pourbahrami