簡體   English   中英

php - 帶有 unicode 的 htmlspecialchars

[英]php - htmlspecialchars with unicode

    $string = "Główny folder grafik<p>asd nc</p>";

echo htmlspecialchars($string);

在現場

G&#322;ówny folder grafik<p>asd nc</p>

在本地

Główny folder grafik<p>asd nc</p>

什么是問題? 我希望在實時站點上運行時結果看起來像本地

htmlspecialchars()接受額外的參數——第三個是字符集。

嘗試指定第三個參數。

您需要向 htmlspecialchars() 函數添加額外的參數。 以下應該工作:

htmlspecialchars($string, ENT_QUOTES, "UTF-8");

您可能希望將一個可選參數傳遞給htmlspecialchars關於字符集,默認情況下為 ISO-8859-1。

如果您需要翻譯所有具有關聯命名實體的字符串,請改用htmlentities() ,該函數在所有方面都與htmlspecialchars()相同,除了htmlentities()所有具有 HTML 字符實體等效項的字符都被翻譯成這些實體。

但即使是htmlentities()也不會對所有unicode 字符進行編碼。 它編碼它可以[所有的latin1],而其他的則通過(例如`Љ)。

此函數會查詢 ansii 表以自定義包含/省略您想要/不想要的字符。

(注意:肯定沒有那么快)

/**
 * Unicode-proof htmlentities.
 * Returns 'normal' chars as chars and weirdos as numeric html entites.
 * @param  string $str input string
 * @return string      encoded output
 */
function superentities( $str ){
    // get rid of existing entities else double-escape
    $str = html_entity_decode(stripslashes($str),ENT_QUOTES,'UTF-8');
    $ar = preg_split('/(?<!^)(?!$)/u', $str );  // return array of every multi-byte character
    foreach ($ar as $c){
        $o = ord($c);
        if ( (strlen($c) > 1) || /* multi-byte [unicode] */
            ($o <32 || $o > 126) || /* <- control / latin weirdos -> */
            ($o >33 && $o < 40) ||/* quotes + ambersand */
            ($o >59 && $o < 63) /* html */
        ) {
            // convert to numeric entity
            $c = mb_encode_numericentity($c,array (0x0, 0xffff, 0, 0xffff), 'UTF-8');
        }
        $str2 .= $c;
    }
    return $str2;
}

暫無
暫無

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

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