解码 php 中的 xml 字符串

decode xml string in php

我想解码这个 xml 字符串

$string= "%3C%3Fxml%20version%3D%221.0%22%20encoding%3D%22UTF-8%22%3F%3E%20%3CPrintStudentData%20%20name%3D%22John%20Doe%22%20marks%3D%2298%22%20%2F%3E";

我试过 echo urldecode($string); 但它没有打印任何东西。

预期输出

<?xml version="1.0" encoding="UTF-8"?> <PrintStudentData  name="John Doe" marks="98" />

尝试

function utf8_urldecode($str) {
    $str = preg_replace("/%u([0-9a-f]{3,4})/i","&#x\1;",urldecode($str));
    return html_entity_decode($str,null,'UTF-8');;
  }

@Michael Scofield 只需添加 header("Content-Type: application/xml") 即可获得所需的输出,如下面的代码所示,因为您必须告诉浏览器我将看到 xml 类型的内容通过 header("Content-Type: application/xml") 你可以在浏览器中看到 xml 输出:

<?php
$string = "%3C%3Fxml%20version%3D%221.0%22%20encoding%3D%22UTF-8%22%3F%3E%20%3CPrintStudentData%20%20name%3D%22John%20Doe%22%20marks%3D%2298%22%20%2F%3E";
$xml = urldecode ($string);
header("Content-Type: application/xml");
echo $xml;