【问题标题】:PHP - Getting an Hex Character code #x2f in strings instead of its symbol "/" (Slash)PHP - 在字符串中获取十六进制字符代码 #x2f 而不是其符号“/”(斜杠)
【发布时间】:2017-03-21 09:24:54
【问题描述】:

我已经使用 php 生成了 xml 文件,但问题是我在内容描述中得到 #x2f 十六进制字符代码而不是其符号斜杠 (/)。

Here 是该 xml 文件的 url。你会看到#x2f;描述中的代码。

代码截图:

我正在尝试生成工作的 RSS 提要。

我试过了:

htmlentities(), htmlspecialchars_decode(), html_entity_decode, iconv()

php 字符串函数但不成功。

我也搜索过 Stack 之前的问题,但没有成功。

感谢您的热心帮助。

编辑1(PHP文件代码):

    header('Content-Type: text/xml');
echo '<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0">
<channel>
<title>Hastings Jobs</title>
<description>Latest job vacancies in Hastings and St Leonards, East Sussex</description>
<link>http://hastingsjobs.net</link>
<pubDate>'.gmdate("D, j M Y h:i:s A").'</pubDate>';

    $JobQuery = new JobApis\Jobs\Client\Queries\ZiprecruiterQuery([
                        'api_key' => "APIKEY"
                    ]);
    $JobQuery->set('jobs_per_page', '10')->set('location','Hastings, UK')->set('radius_miles','5')->set('days_ago',2);
    $client = new JobApis\Jobs\Client\Providers\ZiprecruiterProvider($JobQuery);
    $jobs = $client->getJobs();
    $i=1;
    foreach($jobs->all() as $result)
    {
    $snippet=str_replace("&", "and", $result->__get('description'));
    $jobtitle=str_replace("&", "and", $result->__get('name'));
    $date = $result->__get('datePosted');
$date->setTimezone(new DateTimeZone('Europe/London'));
    $date->modify('-5 hours');
    echo '<item><title>'.$jobtitle.'</title><description>'.htmlentities($snippet, ENT_XML1).'</description><guid>'.htmlEntities($date->format('D, d M Y, H:i:s T')).'</guid><link>'.htmlEntities($result->__get('url')).'</link><pubDate>'.$date->format('D, d M Y, H:i:s T').'</pubDate></item>';
     $i=$i+1;
     if($i==11) break;
}
echo '</channel>
</rss>';

【问题讨论】:

  • 你是如何生成文件的?
  • 带标题 - header('Content-Type: text/xml');
  • 标头不生成 XML。你说 “我已经使用 PHP 生成了 XML 文件” 我要问的是你是怎么做到的。
  • 是的,我正在使用 php 标头创建一个 xml 文件,它告诉编译器将内容编译为 XML。我已将 url 放入以 XML 格式输出的 php 文件中。
  • 所以您不是真正生成您从其他地方获取的 XML 的人?除了header 位之外,您使用的完整代码是什么?

标签: php xml string character-encoding html-entities


【解决方案1】:

htmlentitiesENT_XML1 标志一起使用

htmlentities($string, ENT_XML1);

【讨论】:

    【解决方案2】:

    我遇到了同样的问题并使用了html_entity_decode

    <?php
    echo html_entity_decode($string);
    ?>
    

    【讨论】:

      猜你喜欢
      • 2021-06-26
      • 2019-05-31
      • 1970-01-01
      • 1970-01-01
      • 2023-04-11
      • 2016-06-11
      • 1970-01-01
      • 2020-11-07
      相关资源
      最近更新 更多