使用PHP从XML文件中删除空白
我有一个脚本,它通过PHP将XML数据附加到XML文件的末尾。唯一的问题是,在我通过PHP脚本添加的每一行XML之后,都会创建一行额外的内容(空白)。有没有一种方法可以在不丢失格式整齐的XML文件的情况下,用PHP从XML文件中删除空白?以下是写入XML文件的PHP代码:使用PHP从XML文件中删除空白,php,xml,Php,Xml,我有一个脚本,它通过PHP将XML数据附加到XML文件的末尾。唯一的问题是,在我通过PHP脚本添加的每一行XML之后,都会创建一行额外的内容(空白)。有没有一种方法可以在不丢失格式整齐的XML文件的情况下,用PHP从XML文件中删除空白?以下是写入XML文件的PHP代码: <?php function formatXmlString($xml) { // add marker linefeeds to aid the pretty-tokeniser (adds a linef
<?php
function formatXmlString($xml) {
// add marker linefeeds to aid the pretty-tokeniser (adds a linefeed between all tag-end boundaries)
$xml = preg_replace('/(>)(<)(\/*)/', "$1\n$2$3", $xml);
// now indent the tags
$token = strtok($xml, "\n");
$result = ''; // holds formatted version as it is built
$pad = 0; // initial indent
$matches = array(); // returns from preg_matches()
// scan each line and adjust indent based on opening/closing tags
while ($token !== false) :
// test for the various tag states
// 1. open and closing tags on same line - no change
if (preg_match('/.+<\/\w[^>]*>$/', $token, $matches)) :
$indent=0;
// 2. closing tag - outdent now
elseif (preg_match('/^<\/\w/', $token, $matches)) :
$pad=0;
// 3. opening tag - don't pad this one, only subsequent tags
elseif (preg_match('/^<\w[^>]*[^\/]>.*$/', $token, $matches)) :
$indent=4;
// 4. no indentation needed
else :
$indent = 0;
endif;
// pad the line with the required number of leading spaces
$line = str_pad($token, strlen($token)+$pad, ' ', STR_PAD_LEFT);
$result .= $line . "\n"; // add to the cumulative result, with linefeed
$token = strtok("\n"); // get the next token
$pad += $indent; // update the pad size for subsequent lines
endwhile;
return $result;
}
function append_xml($file, $content, $sibling, $single = false) {
$doc = file_get_contents($file);
if ($single) {
$pos = strrpos($doc, "<$sibling");
$pos = strpos($doc, ">", $pos) + 1;
}
else {
$pos = strrpos($doc, "</$sibling>") + strlen("</$sibling>");
}
return file_put_contents($file, substr($doc, 0, $pos) . "\n$content" . substr($doc, $pos));
}
$content = "<product><id>3</id><name>Product 3</name><price>63.00</price></product>";
append_xml('prudcts.xml', formatXmlString($content), 'url');
?>
*$/,$token,$matches)):
$indent=4;
// 4. 不需要缩进
其他:
$indent=0;
endif;
//用所需数量的前导空格填充该行
$line=str_pad($token,strlen($token)+$pad',,str_pad_LEFT);
$result.=$line。“\n”;//使用换行符添加到累积结果
$token=strtok(“\n”);//获取下一个令牌
$pad+=$indent;//更新后续行的焊盘尺寸
结束时;
返回$result;
}
函数append_xml($file、$content、$sibling、$single=false){
$doc=文件\获取\内容($file);
若有($单){
$pos=strrpos($doc,“,$pos)+1;
}
否则{
$pos=strrpos($doc,“”)和strlen(“”);
}
返回文件内容($file,substr($doc,0,$pos)。“\n$content.substr($doc,$pos));
}
$content=“3Product 363.00”;
追加xml('prudcts.xml',formatXmlString($content),'url');
?>
相反,不要在$result
中添加新数据,然后添加换行符
使用类似于
if(!empty($result)){result.=“\n”}
的方法避免XML数据以换行开头。不要将所有内容都放在一行中,这样更灵活:
return file_put_contents($file, substr($doc, 0, $pos) . "\n$content" . substr($doc, $pos));
相反(建议):
注:与字符串函数相比,使用XML可能更直接,并节省XML处理时间。感谢您的回复。您从哪里获得$result变量?我对此有点困惑。从
formatXmlString()
函数中,我刚刚意识到您并没有编写该函数。您可能应该使用。我刚刚尝试了您的新代码,但在每次XML更新后仍会收到空白/新行。还有其他建议吗?可能是的。您需要修剪
右侧零件以删除多余的换行符。例如,修剪开头、修剪结尾,甚至修剪内容-取决于您的需要:-我只是将rtrim
放在$buffer
周围,因为从阅读您的问题时,我感觉您担心文件末尾的换行。谢谢,通过向函数中的contents变量添加rtrim,解决了这个问题!“//添加标记换行符以辅助漂亮的标记器(在所有标记结束边界之间添加换行符)”-我建议删除该函数的该部分。您不使用DOMDocument的原因是什么?
$buffer = substr($doc, 0, $pos) . "\n$content" . substr($doc, $pos);
$buffer = rtrim($buffer);
return file_put_contents($file, $buffer);