2012-05-10 47 views
1

我有用戶輸入並使用htmlentities()來轉換所有實體。 但是,似乎有一些錯誤。當我在php編碼問題htmlentities

ääää öööö üüüü ääää 

型我得到

ääää öööö üüüü ääää 

它看起來像這樣

ääää à ¶Ã ¶Ã ¶Ã ¶Ã¼Ã¼Ã¼Ã¼ ääää

我在做什麼錯?該代碼是真的只有這個:

$post=htmlentities($post); 

編輯1

下面是一些我使用的格式的目的(也有一些有用的功能,但它們)更多的代碼:

//Secure with htmlentities (mysql_real_escape_string() comes later) 
    $post=htmlentities($post); 

    //Strip obsolete white spaces 
    $post = preg_replace("/ +/", " ", $post); 

    //Detect links 
    $pattern_url='~(?>[a-z+]{2,}://|www\.)(?:[a-z0-9]+(?:\.[a-z0-9]+)[email protected])?(?:(?:[a-z](?:[a-z0-9]|(?<!-)-)*[a-z0-9])(?:\.[a-z](?:[a-z0-9]|(?<!-)-)*[a-z0-9])+|(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?))(?:/[^\\/:?*"<>|\n]*[a-z0-9])*/?(?:\?[a-z0-9_.%]+(?:=[a-z0-9_.%:/+-]*)?(?:&[a-z0-9_.%]+(?:=[a-z0-9_.%:/+-]*)?)*)?(?:#[a-z0-9_%.]+)?~i'; 
    preg_match_all($pattern_url, $post, $matches); 
    for ($i=0; $i < count($matches[0]); $i++) 
    { 
     if(substr($matches[0][$i],0,4)=='www.') 
     $post = str_replace($matches[0][$i],'http://'.$matches[0][$i],$post); 
    } 
    $post = preg_replace($pattern_url,'<a target="_blank" href="\\0">\\0</a>',$post); 

    //Keep line breaks (more than one will be stripped above) 
    $post=nl2br($post); 

    //Remove more than one linebreak 
    $post=preg_replace("/(<br\s*\/?>\s*)+/", "<br/>", $post); 

    //Secure with mysql_real_escape_string() 
    $post=mysql_real_escape_string($post); 
+1

當你說「真的只有這個」時,你能分享它的其餘部分嗎?我沒有看到你的PHP有什麼問題,所以這個問題可能在別的地方。 –

+0

@stevether請參閱問題編輯。 – weltschmerz

回答

7

你必須手動htmlentities()指定編碼(UTF-8):

echo htmlentities("ääää öööö üüüü ääää", null, "UTF-8"); 

輸出:

ääää öööö üüüü ääää 
+0

謝謝!那是我需要的。參數null做什麼?你也許知道我爲什麼需要這個?我通常只使用htmlemtities('字符串'),沒有任何額外的參數,它通常工作正常。 – weltschmerz

+2

它只是說使用第二個參數的默認值。參數2和參數3是可選的,但是如果您想指定第三參數,則必須指定第二參數。將相當於'htmlentities(「string」,ENT_COMPAT | ENT_HTML401,「UTF-8」)' – SupremeDud

+0

謝謝!奇蹟般有效。 – weltschmerz

2

htmlentities的第3個參數與使用該帖子的字符集匹配很重要。我supouse,你是不是submiting UTF8,因爲它是在表中ヶ輛

在PHP

$post = htmlentities ($post, ENT_COMPAT, 'ISO-8859-1') // or whatever 

默認

<form action="your.php" accept-charset="ISO-8859-1"> 

反正actualy我建議你使用UTF8

+0

我這樣做,謝謝:) – weltschmerz