【问题标题】:htmlentities converts trademark into â�¢ [duplicate]htmlentities 将商标转换为†[重复]
【发布时间】:2012-02-24 14:29:53
【问题描述】:

可能重复:
PHP htmlentities() not working as expected

我正在使用 htmlentities 将商标符号转换为 htmlentity,但它给了我â�¢。我是不是做错了什么

这是我的代码

<?php echo '<?xml version="1.0" encoding="utf-8"?>'; ?>
<rss version="2.0" xmlns:g="http://base.google.com/ns/1.0" xmlns:c="http://www.base.google.com/cns/1.0">
<channel>
<title>Spray Foam Systems</title>
<link>http://www.sprayfoamsys.com/store/</link>
<description>Spray Foam Rigs, Spray Foam Equipment, Sprayfoam Parts and Supplies.</description>
<?php
$con = mysql_connect(REMOVED) or die(mysql_error());
    if (!$con)
        {
            die('Could not connect: ' . mysql_error());
        }
    mysql_select_db("sprayfoa_store", $con);
    mysql_query("SET NAMES 'utf8'", $con);

    $query = mysql_query("SELECT * FROM `catalog_product_flat_1` WHERE `visibility` = 4 ORDER BY entity_id asc")
    or die(mysql_error());
?>
<?php
    while($row = mysql_fetch_array($query))
        {
?>
<item>
<g:id><?php echo $row['entity_id']; ?></g:id>
<title><?php echo htmlentities($row['name']); ?></title>
<description><?php echo htmlentities(str_replace(array("\r\n", "\n"), ' ', $row['short_description'])); ?></description>
<g:google_product_category>Business &amp; Industrial &gt; Construction</g:google_product_category>
<g:product_type>Spray Foam Parts &amp; Supplies &gt; Fusion AP Parts</g:product_type>
<link>http://sprayfoamsys.com/store/<?php echo $row['url_path']; ?></link>
<g:image_link>http://sprayfoamsys.com/store/media/catalog/product<?php echo $row['small_image']; ?></g:image_link>
<g:condition>new</g:condition>
<g:availability>in stock</g:availability>
<g:price><?php echo $row['price']; ?></g:price>
<g:brand><?php $entity_id = $row['entity_id']; $query2 = mysql_query("SELECT * FROM `catalog_product_entity_varchar` WHERE entity_id = '$entity_id' AND attribute_id = '127'") or die(mysql_error()); while($row2 = mysql_fetch_array($query2)) { echo $row2['value']; } ?></g:brand>
<g:mpn><?php echo $row['sku']; ?></g:mpn>
</item>
<?php
}
mysql_close($con);
?>
</channel>
</rss>

【问题讨论】:

  • @Brett 你真的,真的应该使用 DOMDocument 来构建 XML。它可能需要更多代码,但像现在这样构建 XML 是完全错误的。另外,了解一下 SQL 连接,循环内的这个查询是完全没有必要的。

标签: php xml html-entities


【解决方案1】:

根据the manual,当不向此函数提供 ISO-8859-1 字符串时,您需要提供一个字符集:

echo htmlentities($row['name'], ENT_QUOTES, 'UTF-8');

2015 年 3 月更新:请注意,自此答案以来,此函数使用的默认字符集已针对不同版本进行了更改:

  • PHP ISO-8859-1
  • PHP 5.4 和 5.5:UTF-8
  • PHP >= 5.6:default_charset 设置(非常受欢迎,一开始就应该如此)。

【讨论】:

  • 好魔法!为我工作,一个 PHP 菜鸟。
【解决方案2】:

尝试探索htmlentities()的附加参数,它们可能会有所帮助

http://bg2.php.net/htmlentities

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-06-15
    • 2011-07-28
    • 1970-01-01
    • 1970-01-01
    • 2016-01-09
    • 2017-04-27
    • 1970-01-01
    相关资源
    最近更新 更多