簡體   English   中英

str_replace PHP不起作用

[英]str_replace PHP doesn't work

我試圖用某些其他字符串替換某些字符,但我卻用這個確切的代碼在我的主頁上工作,但在另一頁上它將無法工作。

在數據庫中,我得到了±1¢2£¥3§4£作為description2在我的PHP中,我使用

$description2 = mysql_result($product, 0, 'productDescription2');

if ($description2 !=""){
$searchArray = array('±', '¢', '£', '¥', '§');
$replaceArray = array('x', 'x', 'x', 'x', 'x');
$teknisk =  str_replace($searchArray, $replaceArray, $description2);}

作為輸出,我使用echo ''. $teknisk .''; echo ''. $teknisk .'';

但是結果是 1 2 3 4 (與原始description2相同)

而不是x1x2xx3x4x

請幫忙!

也許數據庫使用的編碼與PHP文件使用的編碼不同。 首先比較用bin2hex()函數(在PHP中)轉換的那些字符串。

第二次嘗試:

PHP文檔的注釋中有很多建議。 試試這些:

  1. http://www.php.net/manual/es/function.str-replace.php#106653
  2. http://www.php.net/manual/es/function.str-replace.php#75566
  3. http://www.php.net/manual/es/function.str-replace.php#72344

嘗試顯式設置頁面的字符類型。 嘗試將其放在標題中:

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />

或者,如果您使用的是HTML 5:

<meta charset="utf-8">

我認為,由於您有一些奇怪的字符,它們可能超出了ascii的范圍,並且您可能需要使用多字節字符串函數,盡管它沒有str_replace類似物,所以您可能需要使用多字節正則表達式。

請看一下,它將告訴您如何在網絡應用中處理UTF字符

Web Apps中的UTF-8

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM