使用PHP解码多个xml标签

 海纳百川2602902033 发布于 2023-02-11 12:09

我正在寻找一种解码字符串中多个XML标签的"智能方法",我有以下功能:

function b($params) {
    $xmldata = '' . html_entity_decode($params['data']) . '';
    $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'] = 'Service DNSDNS Service - DNS GratuitFree DNS';
$params['lang'] = 'French';
$a = b($params);
print_r($a);

但是产出:

Service DNS

我希望它基本上输出每个标签,因此结果应该是:

Service DNS - DNS Gratuit

拉出我的头发.任何快速帮助或指示将不胜感激.


编辑:优化需求.

似乎我不够清楚; 让我举一个例子

如果我有以下字符串作为输入:

The ChatCat is very happy to stay on stackoverflow 
because it makes him HeureuxHappy to know that it 
is the best EndroitPlace to find good people with
good RéponsesAnswers.

因此,如果我使用'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.

希望现在更清楚了.

撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有