簡體   English   中英

使用php將特殊字符轉換為html代碼

[英]convert special characters to html code with php

我需要一個可以清除字符串特殊字符的函數。 希望這樣將<br />類的HTML字符轉換為&lt;br /&gt;

我想將以下內容轉換為:•,½,'到html代碼。

這是我當前使用的功能,但似乎不適用於分數。

function cleanText($str){

$str = str_replace("Ñ" ,"&#209;", $str);
$str = str_replace("ñ" ,"&#241;", $str);
$str = str_replace("ñ" ,"&#241;", $str);
$str = str_replace("Á","&#193;", $str);
$str = str_replace("á","&#225;", $str);
$str = str_replace("É","&#201;", $str);
$str = str_replace("é","&#233;", $str);
$str = str_replace("ú","&#250;", $str);
$str = str_replace("ù","&#249;", $str);
$str = str_replace("Í","&#205;", $str);
$str = str_replace("í","&#237;", $str);
$str = str_replace("Ó","&#211;", $str);
$str = str_replace("ó","&#243;", $str);
$str = str_replace("“","&#8220;", $str);
$str = str_replace("”","&#8221;", $str);

$str = str_replace("‘","&#8216;", $str);
$str = str_replace("’","&#8217;", $str);
$str = str_replace("—","&#8212;", $str);

$str = str_replace("–","&#8211;", $str);
$str = str_replace("™","&trade;", $str);
$str = str_replace("ü","&#252;", $str);
$str = str_replace("Ü","&#220;", $str);
$str = str_replace("Ê","&#202;", $str);
$str = str_replace("ê","&#238;", $str);
$str = str_replace("Ç","&#199;", $str);
$str = str_replace("ç","&#231;", $str);
$str = str_replace("È","&#200;", $str);
$str = str_replace("è","&#232;", $str);
$str = str_replace("•","&#149;" , $str);

$str = str_replace("¼","&#188;" , $str);
$str = str_replace("½","&#189;" , $str);
$str = str_replace("¾","&#190;" , $str);
$str = str_replace("½","&#189;" , $str);

return $str;

}

您可以使用ENT_SUBSTITUTE屬性將整個函數替換為htmlentities 除了可以正常工作外,它的執行速度也快得多。

注意: ENT_SUBSTITUTE自PHP 5.4 ENT_SUBSTITUTE可用。

猜猜是時候看看htmlentities PHP函數及其選項了。

基本上,您可以將整個功能替換為:

$str = htmlentities( $str );

效率也會大大提高。

如果需要特殊處理(尤其是ENT_SUBSTITUTE ),請務必看一下該函數的可選參數。

$str = htmlentities( $str, ENT_SUBSTITUTE );

嘗試一下,我已經使用此函數將任何內容轉換為unicode:

class unicode_replace_entities {
public function UTF8entities($content="") {
    $contents = $this->unicode_string_to_array($content);
    $swap = "";
    $iCount = count($contents);
    for ($o=0;$o<$iCount;$o++) {
        $contents[$o] = $this->unicode_entity_replace($contents[$o]);
        $swap .= $contents[$o];
    }
    return mb_convert_encoding($swap, "UTF-8"); //not really necessary, but why not.
}
public function unicode_string_to_array( $string ) { //adjwilli
    $strlen = mb_strlen($string);
    while ($strlen) {
        $array[] = mb_substr( $string, 0, 1, "UTF-8" );
        $string = mb_substr( $string, 1, $strlen, "UTF-8" );
        $strlen = mb_strlen( $string );
    }
    return $array;
}
public function unicode_entity_replace($c) { //m. perez
    $h = ord($c{0});
    if ($h <= 0x7F) {
        return $c;
    } else if ($h < 0xC2) {
            return $c;
        }

    if ($h <= 0xDF) {
        $h = ($h & 0x1F) << 6 | (ord($c{1}) & 0x3F);
        $h = "&#" . $h . ";";
        return $h;
    } else if ($h <= 0xEF) {
            $h = ($h & 0x0F) << 12 | (ord($c{1}) & 0x3F) << 6 | (ord($c{2}) & 0x3F);
            $h = "&#" . $h . ";";
            return $h;
        } else if ($h <= 0xF4) {
            $h = ($h & 0x0F) << 18 | (ord($c{1}) & 0x3F) << 12 | (ord($c{2}) & 0x3F) << 6 | (ord($c{3}) & 0x3F);
            $h = "&#" . $h . ";";
            return $h;
        }
}
}

$oUnicodeReplace = new unicode_replace_entities();

$oUnicodeReplace->UTF8entities($string);

請注意,它將轉換所有內容,但否則將處理奇怪的字符……不是我自己的腳本,但我也不知道我在哪里找到它。

是: http ://www.php.net/manual/en/function.htmlentities.php或這樣: http : //www.php.net/manual/en/function.htmlspecialchars.php

暫無
暫無

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

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