【发布时间】:2014-02-10 17:56:47
【问题描述】:
您好,我想知道如何正确使用 php。
- 加载 XML 文档
- 查找和替换文本
- 以新名称保存 xml
XML 数据在这里无法发布,原因不明。 http://pastebin.com/A3rDtwzp
我想在内容标签之间找到文本,并将其替换为当前年份,用于 xml 中的所有标签。
如果可以请指导我简单的方法谢谢。
谢谢!
【问题讨论】:
您好,我想知道如何正确使用 php。
XML 数据在这里无法发布,原因不明。 http://pastebin.com/A3rDtwzp
我想在内容标签之间找到文本,并将其替换为当前年份,用于 xml 中的所有标签。
如果可以请指导我简单的方法谢谢。
谢谢!
【问题讨论】:
加载 XML 文档
<?php
// Issues an E_STRICT error
$doc = DOMDocument::loadXML('<root><node/></root>');
echo $doc->saveXML();
?>
查找和替换文本
<?php
// The text string
$text = "The quick brown fox jumped over the lazy dog.";
// The word we want to replace
$oldWord = "brown";
// The new word we want in place of the old one
$newWord = "blue";
// Run through the text and replaces all occurrences of $oldText
$text = str_replace($oldWord , $newWord , $text);
// Display the new text
echo $text;
?>
答案为 3
<?php
$docfile = new DOMDocument( );
$eleT = $docfile->createElement( 'Root' );
$eleT->nodeValue = 'Hello XML World';
$docfile->appendChild( $eleT );
$docfile->save('MyXmlFile.xml');
?>
【讨论】: