php SimpleXml 到字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3690942/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
SimpleXml to string
提问by liysd
Is there any function that makes string from PHP SimpleXMLElement
?
是否有任何函数可以从 PHP 生成字符串SimpleXMLElement
?
回答by Tim Cooper
You can use the SimpleXMLElement::asXML()
method to accomplish this:
您可以使用该SimpleXMLElement::asXML()
方法来完成此操作:
$string = "<element><child>Hello World</child></element>";
$xml = new SimpleXMLElement($string);
// The entire XML tree as a string:
// "<element><child>Hello World</child></element>"
$xml->asXML();
// Just the child node as a string:
// "<child>Hello World</child>"
$xml->child->asXML();
回答by Nicolay77
You can use casting:
您可以使用铸造:
<?php
$string = "<element><child>Hello World</child></element>";
$xml = new SimpleXMLElement($string);
$text = (string)$xml->child;
$text will be 'Hello World'
$text 将是“Hello World”
回答by codaddict
回答by tolginho
Actually asXML() converts the string into xml as it name says:
实际上 asXML() 将字符串转换为 xml,正如它的名字所说:
<id>5</id>
This will display normally on a web page but it will cause problems when you matching values with something else.
这将正常显示在网页上,但是当您将值与其他内容匹配时会导致问题。
You may use strip_tags function to get real value of the field like:
您可以使用 strip_tags 函数来获取字段的实际值,例如:
$newString = strip_tags($xml->asXML());
PS: if you are working with integers or floating numbers, you need to convert it into integer with intval()or floatval().
PS:如果您正在处理整数或浮点数,则需要使用intval()或floatval()将其转换为整数。
$newNumber = intval(strip_tags($xml->asXML()));
回答by Leander
You can use ->child
to get a child element named child.
您可以使用->child
来获取名为child的子元素。
This element will contain the text of the childelement.
此元素将包含子元素的文本。
But if you try var_dump()
on that variable, you will see it is not actually a PHP string.
但是,如果您尝试var_dump()
使用该变量,您会发现它实际上并不是一个 PHP 字符串。
The easiest way around this is to perform a strval(xml->child);
That will convert it to an actual PHP string.
解决此问题的最简单方法是执行strval(xml->child);
That 将其转换为实际的 PHP 字符串。
This is useful when debugging when looping your XML and using var_dump()
to check the result.
在循环 XML 并var_dump()
用于检查结果时进行调试时,这很有用。
So $s = strval($xml->child);
.
所以$s = strval($xml->child);
。
回答by paralaks
Here is a function I wrote to solve this issue (assuming tag has no attributes). This function will keep HTML formatting in the node:
这是我为解决此问题而编写的函数(假设标签没有属性)。此函数将在节点中保留 HTML 格式:
function getAsXMLContent($xmlElement)
{
$content=$xmlElement->asXML();
$end=strpos($content,'>');
if ($end!==false)
{
$tag=substr($content, 1, $end-1);
return str_replace(array('<'.$tag.'>', '</'.$tag.'>'), '', $content);
}
else
return '';
}
$string = "<element><child>Hello World</child></element>";
$xml = new SimpleXMLElement($string);
echo getAsXMLContent($xml->child); // prints Hello World
回答by Luca Reghellin
Sometimes you can simply typecast:
有时您可以简单地进行类型转换:
// this is the value of my $xml
object(SimpleXMLElement)#10227 (1) {
[0]=>
string(2) "en"
}
$s = (string) $xml; // returns "en";
回答by Keenan Stewart
Probablydepending on the XML feed you may/may not need to use __toString()
; I had to use the __toString()
otherwise it is returning the string inside an SimpleXMLElement. Maybe I need to drill down the object further ...
可能取决于您可能/可能不需要使用的 XML 提要__toString()
;我必须使用__toString()
否则它会返回 SimpleXMLElement 内的字符串。也许我需要进一步深入研究对象......