【发布时间】:2015-08-01 20:54:34
【问题描述】:
这是我的 XML 数据:
$data = '<title>Report of the <org reg="International Foo and Bar Conference, 5th">Fifth International Foo and Bar Conference</org>, <org>Foobar Hall</org>, London, July 14 to 16, 1908.</title>';
我可以加载它:
$xml = simplexml_load_string( $data );
print_r( $xml );
这会返回:
SimpleXMLElement Object (
[org] => Array (
[0] => Fifth International Foo and Bar Conference
[1] => Foobar Hall ) )
但现在我可以尝试再次将其放入字符串中:
$flat = (string) $xml;
print_r( $flat );
这就是我所看到的:
Report of the , , London, July 14 to 16, 1908.
但我宁愿是这样:
Report of the Fifth International Foo and Bar Conference, Foobar Hall, London, July 14 to 16, 1908.
有没有一种简单的方法可以用 PHP 做到这一点,而无需显式地递归每个节点?也就是说,有没有一种方法可以将 XML 展平并从中提取所有文本,而不考虑标签?
【问题讨论】:
-
您是否考虑过使用正则表达式从原始字符串中删除所有标签?