gpt4 book ai didi

php - 如何将对象(SimpleXMLElement)转换为字符串

转载 作者:行者123 更新时间:2023-12-03 16:13:01 25 4
gpt4 key购买 nike

我正在使用 xpath 解析网页中的文本,但它将它作为对象返回,我如何将其作为字符串返回。

libxml_use_internal_errors(TRUE);
$dom = new DOMDocument();
$dom->loadHTML($source);
$xml = simplexml_import_dom($dom);
libxml_use_internal_errors(FALSE);
$username = $xml->xpath("//span[@class='user']");

$username 数组的 var_dump:
object(SimpleXMLElement)#3 (2) { ["@attributes"]=> array(1) { ["class"]=> string(4) "user" } [0]=> string(11) "bubblebubble1210" }

最佳答案

list(, $node) = $username;

var_dump($node);
// object(SimpleXMLElement)#3 (1) { [0]=> string(11) "bubblebubble1210" }
$node仍将是上面的对象,但您可以使用 (string) 显式转换它或使用 echo这将隐式地转换它。

CodePad .

关于php - 如何将对象(SimpleXMLElement)转换为字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5319640/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com