【发布时间】:2013-12-30 20:00:01
【问题描述】:
我正在寻找一种在字符串中解码多个 XML 标记的“智能方式”,我有以下功能:
function b($params) {
$xmldata = '<?xml version="1.0" encoding="UTF-8" ?><root>' . html_entity_decode($params['data']) . '</root>';
$lang = ucfirst(strtolower($params['lang']));
if (simplexml_load_string($xmldata) === FALSE) {
return $params['data'];
} else {
$langxmlobj = new SimpleXMLElement($xmldata);
if ($langxmlobj -> $lang) {
return $langxmlobj -> $lang;
} else {
return $params['data'];
}
}
}
尝试一下
$params['data'] = '<French>Service DNS</French><English>DNS Service</English> - <French>DNS Gratuit</French><English>Free DNS</English>';
$params['lang'] = 'French';
$a = b($params);
print_r($a);
但是输出:
Service DNS
我希望它基本上输出每个标签,所以结果应该是:
Service DNS - DNS Gratuit
拔掉我的头发。任何快速帮助或指示将不胜感激。
编辑:细化需求。
好像我说的不够清楚;让我再举一个例子
如果我有以下字符串作为输入:
The <French>Chat</French><English>Cat</English> is very happy to stay on stackoverflow
because it makes him <French>Heureux</French><English>Happy</English> to know that it
is the best <French>Endroit</French><English>Place</English> to find good people with
good <French>Réponses</French><English>Answers</English>.
所以如果我用'French'运行函数,它将返回:
The Chat is very happy to stay on stackoverflow
because it makes him Heureux to know that it
is the best Endroit to find good people with
good Réponses.
还有“英语”:
The Cat is very happy to stay on stackoverflow
because it makes him Happy to know that it
is the best Place to find good people with
good Answers.
希望现在更清楚了。
【问题讨论】:
-
你的 php 版本是什么?您的代码为我输出每个标签($a 是 SimpleXMLElement 对象)
标签: php xml-parsing