我认为问题在于我的逻辑,我可能会以错误的方式解决这个问题。我想要的是
a
替换为ა
b
ბ
,依此类推。这是我所拥有的代码,但它不起作用。
xmlDoc=loadXMLDoc("temp/word/document.xml");
$nodes = xmlDoc.getElementsByTagName("w:t");
foreach ($nodes as $node) {
while( $node->hasChildNodes() ) {
$node = $node->childNodes->item(0);
}
$node->nodeValue = str_replace("a","ა",$node->nodeValue);
$node->nodeValue = str_replace("b","ბ",$node->nodeValue);
$node->nodeValue = str_replace("g","გ",$node->nodeValue);
$node->nodeValue = str_replace("d","დ",$node->nodeValue);
// More replacements for each letter in the alphabet.
}
我认为这可能是因为多个str_replace()
调用,但即使只有一个调用也无效。我是以错误的方式解决这个问题还是错过了什么?
答案 0 :(得分:1)
$node
变量在每次迭代时都会被覆盖,因此只会修改最后一个$node
(如果有的话)。您需要在循环内部进行替换,然后使用saveXML()
方法返回修改后的XML标记。
您的代码(有一些改进):
$xmlDoc = new DOMDocument();
$xmlDoc->load('temp/word/document.xml');
foreach ($xmlDoc->getElementsByTagName("w:t") as $node) {
while($node->hasChildNodes()) {
$node = $node->childNodes->item(0);
$search = array('a', 'b', 'g', 'd');
$replace = array('ა', 'ბ', 'გ', 'დ');
$node->nodeValue = str_replace($search, $replace, $node->nodeValue);
}
}
echo $xmlDoc->saveXML();