2012-02-01 50 views
0

可能重複:
PHP htmlentities() not working as expectedヶ輛轉換成商標â ¢

我使用ヶ輛的商標符號轉換成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> 
+0

你需要第三個參數['ヶ輛($海峽,$標誌, 「UTF-8」)'] (http://php.net/htmlentities) – mario 2012-02-01 21:45:56

+0

@Brett你真的*真*應該使用DOMDocument來構建XML。這可能是更多的代碼,但是像現在這樣構建XML顯然是錯誤的。另外,瞭解有關SQL連接的信息,循環內的這個查詢是完全不必要的。 – Tomalak 2012-02-01 21:52:49

回答

10

the manual,當不攝食與ISO-8859-1的字符串此功能,您需要提供一個字符集:

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

2015年3月更新:請注意,由於此答案,此功能使用的默認字符集已針對不同版本進行了更改:

  • PHP < 5.4:ISO-8859-1
  • PHP 5.4 & 5.5:UTF-8
  • PHP> = 5.6:該default_charset設置(這是非常受歡迎的,以及它如何應該已經在第一位置)。
+0

不錯的魔法!適用於我,PHP noob。 – felixwcf 2016-11-11 08:05:23