2012-04-04 124 views
0

首先,字符串正在從XML文件中提取。str_replace的問題

有,我試圖取代一個特殊字符: '£'

當我使用str_replace函數像這樣:

$ability1 = str_replace("£", "", $ability); 

這是什麼的var_dump顯示:

string(138) "Argothian Pixies can't be blocked by artifact creatures.�Prevent all damage that would be dealt to Argothian Pixies by artifact creatures." 

一旦$ ability1被傳遞,wordpress將其插入帖子中。這是結果。

Argothian Pixies can’t be blocked by artifact creatures. 

刪除 字符後的所有內容。

爲什麼當£應該是「」時,將£改變爲 。我不太確定我丟失的是什麼

回答

1

也許你的字符串是UTF-8? PHP。你將不得不這樣做:

$ability1 = utf8_decode($ability); 
$ability1 = preg_replace("/[£ ]/","", $ability1); 
$ability1 = utf8_encode($ability1); 
+1

是的,就是這樣。我前幾天讀過那篇文章,但它已經滑了我的頭腦,哈哈。 preg_replace導致它刪除所有內容,只留下特殊字符。結束使用: $ ability1 = utf8_decode($ ability); $ ability1 = str_replace(「£」,「」,$ ability1); $ ability1 = utf8_encode($ ability1); – 2012-04-04 13:30:15

+0

對不起。表達中有一個「不」(^)。 – fragmentedreality 2012-04-04 13:47:55

+0

**使用警告:**這段代碼引入了數據丟失。 – hakre 2012-06-15 16:18:37

1

XML文件是如何編碼的?我懷疑它可能是UTF-8。在這種情況下,你需要看到一個函數,如utf_decode()在你的代碼中正確處理它(假設你的代碼是ANSI)