PHP gzread、gzfile、gzopen 等。所有标签都从 XML 和 return 中剥离,只有值
PHP gzread, gzfile, gzopen, etc.. all strip tags off of XML and return only the values
我有包含 xml 个文件的 .gz 文件。我已经尝试了下面代码中显示的所有不同内容的每种组合。任何时候 gz..... 方法之一“工作”它 returns XML 文件中包含的值将所有标签和元数据都消失。例如,如果 xml 文件如下所示:
<?xml version="1.0" encoding="UTF-8" ?>
<tag1>
<taga>
This
</taga>
<tagb>
is the stuff
</tagb>
</tag1>
<tag2>
<taga>
I get but only
</taga>
<tagb>
This
</tagb>
</tag2>
我得到的是:
This is the stuff I get but only This
代码如下:
<?php
$mailfileObj->zipfile = 'path/to/gzfile.gz'; //ignore the fact that it says zipfile, it is a .gz file
try{
$opengzfile = gzopen($mailfileObj->zipfile, "r");
$contents = gzread($opengzfile, filesize($mailfileObj->zipfile));
gzclose($opengzfile);
var_dump($contents);
echo '<br>';
//$opengzfile = fopen($mailfileObj->zipfile, "r");
//$contents = fread($opengzfile, filesize($mailfileObj->zipfile));
//fclose($opengzfile);
//$contents = file_get_contents($mailfileObj->zipfile);
$contents2 = '';
$lines = gzfile($mailfileObj->zipfile);
foreach ($lines as $line) {
echo $line;
$contents2 = $contents2.$line;
}
//var_dump($contents);
//echo '<br>';
//var_dump($contents);
//echo $contents . '<br><br>';
//$xmlfilegz = $mailfileObj->filename.'.xml';
//$openxmlfile = fopen($xmlfilegz, "w");
//fwrite($openxmlfile, $contents);
//fclose($openxmlfile);
$opengzfile = fopen($mailfileObj->zipfile, "r");
$contents2 = fread($opengzfile, filesize($mailfileObj->zipfile));
fclose($opengzfile);
//$contents2 = file_get_contents($mailfileObj->zipfile);
//$contents2 = gzdecode($contents);
$contents2 = gzinflate($contents);
//$contents2 = gzuncompress($contents);
var_dump($contents2);
}
catch(Exception $e){
echo 'Caught exception: ' . $e->getMessage() . '<br>';
}
?>
这里有什么问题?我错过了什么?
谢谢。
您将 XML 放在 HTML 网页中,因此浏览器会将 XML 标签解释为 HTML 标签。
使用 htmlentities()
对其进行编码,以便按字面意思呈现。
foreach ($lines as $line) {
echo htmlentities($line);
$contents2 = $contents2.$line;
}
您可能希望在 <pre>
块中显示此内容,以便保留换行符和缩进。
我有包含 xml 个文件的 .gz 文件。我已经尝试了下面代码中显示的所有不同内容的每种组合。任何时候 gz..... 方法之一“工作”它 returns XML 文件中包含的值将所有标签和元数据都消失。例如,如果 xml 文件如下所示:
<?xml version="1.0" encoding="UTF-8" ?>
<tag1>
<taga>
This
</taga>
<tagb>
is the stuff
</tagb>
</tag1>
<tag2>
<taga>
I get but only
</taga>
<tagb>
This
</tagb>
</tag2>
我得到的是:
This is the stuff I get but only This
代码如下:
<?php
$mailfileObj->zipfile = 'path/to/gzfile.gz'; //ignore the fact that it says zipfile, it is a .gz file
try{
$opengzfile = gzopen($mailfileObj->zipfile, "r");
$contents = gzread($opengzfile, filesize($mailfileObj->zipfile));
gzclose($opengzfile);
var_dump($contents);
echo '<br>';
//$opengzfile = fopen($mailfileObj->zipfile, "r");
//$contents = fread($opengzfile, filesize($mailfileObj->zipfile));
//fclose($opengzfile);
//$contents = file_get_contents($mailfileObj->zipfile);
$contents2 = '';
$lines = gzfile($mailfileObj->zipfile);
foreach ($lines as $line) {
echo $line;
$contents2 = $contents2.$line;
}
//var_dump($contents);
//echo '<br>';
//var_dump($contents);
//echo $contents . '<br><br>';
//$xmlfilegz = $mailfileObj->filename.'.xml';
//$openxmlfile = fopen($xmlfilegz, "w");
//fwrite($openxmlfile, $contents);
//fclose($openxmlfile);
$opengzfile = fopen($mailfileObj->zipfile, "r");
$contents2 = fread($opengzfile, filesize($mailfileObj->zipfile));
fclose($opengzfile);
//$contents2 = file_get_contents($mailfileObj->zipfile);
//$contents2 = gzdecode($contents);
$contents2 = gzinflate($contents);
//$contents2 = gzuncompress($contents);
var_dump($contents2);
}
catch(Exception $e){
echo 'Caught exception: ' . $e->getMessage() . '<br>';
}
?>
这里有什么问题?我错过了什么?
谢谢。
您将 XML 放在 HTML 网页中,因此浏览器会将 XML 标签解释为 HTML 标签。
使用 htmlentities()
对其进行编码,以便按字面意思呈现。
foreach ($lines as $line) {
echo htmlentities($line);
$contents2 = $contents2.$line;
}
您可能希望在 <pre>
块中显示此内容,以便保留换行符和缩进。