【问题标题】:Find and Replace XML with PHP用 PHP 查找和替换 XML
【发布时间】:2023-03-26 22:22:01
【问题描述】:

我有一个不准确的播客生成器,我正在尝试使用带有此代码的 php 进行更正:

<?php
$superxmlfile_load_file("http://www.podcastgarden.com/podcast/podcast-rss.php?id=2337");
$xml= str_replace(type="audio/mp4", type="video/mp4", $superxmlfile);
echo $newtext
?>

有人知道出了什么问题以及如何解决吗?谢谢!

【问题讨论】:

  • 你错过了报价$xml= str_replace('type="audio/mp4"', 'type="video/mp4"', $superxmlfile);
  • 看起来有很多语法错误。 $superxmlfile_load_file 是什么意思?
  • @Andrew 该脚本有空白输出和错误消息。
  • @bansi $superxmlfile_load_file 是我试图加载 XML 文件并将其保存为变量。

标签: php xml replace find itunes


【解决方案1】:

看起来有很多语法错误。

试试这个。

<?php
$superxmlfile = file_get_contents("http://www.podcastgarden.com/podcast/podcast-rss.php?id=2337");
$xml= str_replace('type="audio/mp4"', 'type="video/mp4"', $superxmlfile);
echo $xml;
?>

您应该至少获得一个带有 PHP 代码语法高亮的基本编辑器。

【讨论】:

  • 您错过了对$superxmlfile 的错误分配并回显了一些不存在的内容
【解决方案2】:

如果你想把那个 xml 变成你可以操作的东西。

首先,使用正确的函数simplexml_load_file()获取xml。

其次,获取原始xml后,将其转换为数组。你可以使用json_encode() 然后json_decode()

第三,既然你已经知道要交换什么键值对,直接覆盖当前转换后的数组即可。

考虑这个例子:

$url = 'http://www.podcastgarden.com/podcast/podcast-rss.php?id=2337';
$xml = simplexml_load_file($url);
$raw_data = json_decode(json_encode($xml), true);
$raw_data['channel']['item']['enclosure']['@attributes']['type'] = 'video/mp4';
echo "<pre>";
print_r($raw_data);
echo "</pre>";

样本输出:

Array
(
    [@attributes] => Array
        (
            [version] => 2.0
        )

    [channel] => Array
        (
            [title] => ACPG
            [link] => http://www.podcastgarden.com/podcast/acpg#2337
            [language] => en-us
            [copyright] => Mean Elephant Productions
            [description] => Broadcast live every month ACPG is definitely one of the best podcasts Owen has starred in.
            [item] => Array
                (
                    [title] => Episode 1
                    [enclosure] => Array
                        (
                            [@attributes] => Array
                                (
                                    [url] => http://www.podcastgarden.com/login/audio-3/3550/ACPGEpisode1.mp4
                                    [type] => video/mp4 // <-- from audio/mp4
                                    [length] => 6000
                                )

                        )

                    [guid] => http://www.podcastgarden.com/episode/episode-1_9845
                    [pubDate] => Sat, 25 Jan 2014 00:00:00 MST
                )

        )

)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-03-27
    • 2016-10-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多