Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/230.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 将html文档中的英文数字转换为阿拉伯文数字_Php_Html_Translation_Digits - Fatal编程技术网

Php 将html文档中的英文数字转换为阿拉伯文数字

Php 将html文档中的英文数字转换为阿拉伯文数字,php,html,translation,digits,Php,Html,Translation,Digits,我想使用PHP函数将某些HTML文档内容中的英文数字(0,1,2,3,…)转换为阿拉伯文数字(۰、۱、۲、۳……)。 我写了这个函数: function en2ar($str) { $ends = array('0','1','2','3','4','5','6','7','8','9'); $ards = array('۰','۱','۲','۳','۴','۵','۶','۷','۸','۹'); return str_replace($ends,$ards,$str

我想使用PHP函数将某些HTML文档内容中的英文数字(0,1,2,3,…)转换为阿拉伯文数字(۰、۱、۲、۳……)。
我写了这个函数:

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>
1
2.
致:

۱
۲
但它转化为:

<h۱ style="color: #۳۳۳;">۱</h۱>
<div style="width: ۱۸۰px;">۲</div>
۱
۲

并使文件无效。

我的评论大致概述如下:

$doc = new DOMDocument();
$doc->loadHTML('<h1 style="color: #333;">1</h1><div style="width: 180px;">2</div>');

$xpath = new DOMXPath($doc);
$textnodes = $xpath->query('//text()');

foreach ($textnodes as $textnode) {
    $textnode->nodeValue = en2ar($textnode->nodeValue);
}

echo $doc->saveHTML();
$doc=newDOMDocument();
$doc->loadHTML('12');
$xpath=新的DOMXPath($doc);
$textnodes=$xpath->query('//text()');
foreach($textnodes作为$textnode){
$textnode->nodeValue=en2ar($textnode->nodeValue);
}
echo$doc->saveHTML();

买主警告:未经测试。算法很简单:加载dom,获取文本节点,每次更改值,保存修改后的dom。

您可以尝试使用类似DOMDocument的HTML解析器

下面是一个例子:

$html = 
'<!DOCTYPE HTML>
<html>
<head></head>
<body>
    <h1 style="color: #333;">1</h1>
    <div style="width: 180px;">2</div>
</body>
</html>';

$doc = new DOMDocument();
$doc->loadHTML($html);
$doc->encoding = 'UTF-8'; //Appropriate encoding HERE
$root = $doc->documentElement;

var_dump($doc->saveHTML());
iterate($root);
var_dump($doc->saveHTML());

function iterate($node)
{
    if($node->nodeType === XML_TEXT_NODE) {
        $node->nodeValue = en2ar($node->nodeValue);
    }
    if ($node->hasChildNodes()) {
        $children = $node->childNodes;
        foreach($children as $child) {
            iterate($child); 
        }
    }
}

使用,然后根据需要更新。离题:哇!我以为阿拉伯数字是1,2,3…使用正则表达式不是更好吗?
$html = 
'<!DOCTYPE HTML>
<html>
<head></head>
<body>
    <h1 style="color: #333;">1</h1>
    <div style="width: 180px;">2</div>
</body>
</html>';

$doc = new DOMDocument();
$doc->loadHTML($html);
$doc->encoding = 'UTF-8'; //Appropriate encoding HERE
$root = $doc->documentElement;

var_dump($doc->saveHTML());
iterate($root);
var_dump($doc->saveHTML());

function iterate($node)
{
    if($node->nodeType === XML_TEXT_NODE) {
        $node->nodeValue = en2ar($node->nodeValue);
    }
    if ($node->hasChildNodes()) {
        $children = $node->childNodes;
        foreach($children as $child) {
            iterate($child); 
        }
    }
}
string '<!DOCTYPE HTML>
<html><head></head><body>
    <h1 style="color: #333;">1</h1>
    <div style="width: 180px;">2</div>
</body></html>
' (length=135)
string '<!DOCTYPE HTML>
<html><head></head><body>
    <h1 style="color: #333;">۱</h1>
    <div style="width: 180px;">۲</div>
</body></html>
' (length=147)