0
<?php
$modcontent = file_get_contents('hello.xml');
preg_match("/<Content(.*?)>(.+?)<\/Content/s", $modcontent, $xmlmatches);
$search  = array('<![CDATA[',']]>');
$replace = array('','');
$thenewone = str_replace($search, $replace, $xmlmatches[2]);
fopen('hello.xml');
fwrite($thenewone);
fclose('hello.xml');
?>

こんにちは、上記のコード (動作しません) から始めて、hello.xml ファイルを変更して、preg_match のように 間のコンテンツのみを保持するようにできますか? hello.xml はhttp://www.google.com/ig/modules/hello.xmlにあります。私の PHP と英語の知識はかなり低いので、別の言葉でもう一度説明します。hello.xml ファイルを開き、Content タグ間のコンテンツを読み取り、そのコンテンツを書き込み、ファイル内の他のすべてを置き換えて、hello.xml ファイルを保存できる PHP スクリプトが必要です。したがって、このすべての操作の後、.xml にはHello, world!のみが含まれている必要があります。. どうもありがとう!

4

1 に答える 1

2

ここで、私のお気に入りのDOMDocumentを使用してこれを試してください。

<?php 
$xml = file_get_contents('http://www.google.com/ig/modules/hello.xml');

$dom = new DOMDocument("1.0","UTF-8");
@$dom->loadXML($xml);
$dom->preserveWhiteSpace = false;
//Get the node by tag then get the textContent/nodeValue
$content = $dom->getElementsByTagName('Content')->item(0)->textContent;

//Hello, world! 
print_r($content);
//Write to file
file_put_contents('hello.txt',$content);
?>
于 2012-08-13T06:20:08.363 に答える