htmlentities

(PHP 4, PHP 5)

htmlentitiesПреобразует все возможные символы в соответствующие HTML-сущности

Описание

string htmlentities ( string $string [, int $flags = ENT_COMPAT | ENT_HTML401 [, string $encoding = 'UTF-8' [, bool $double_encode = true ]]] )

Эта функция идентична htmlspecialchars() за исключением того, что htmlentities() преобразует все символы в соответствющие HTML-сущности (для тех символов, для которых HTML сущности существуют).

Если же вы хотите раскодировать строку (наоборот), используйте html_entity_decode().

Список параметров

string

Входная строка.

flags

Битовая маска из нижеуказанных флагов, определяющих режим обработки кавычек, некорректных кодовых последовательностей и используемый тип документа. По умолчанию используется ENT_COMPAT | ENT_HTML401.

Доступные значения параметра flags
Название константы Описание
ENT_COMPAT Преобразует двойные кавычки, одинарные кавычки не изменяются.
ENT_QUOTES Преобразует как двойные, так и одинарные кавычки.
ENT_NOQUOTES Оставляет без изменения как двойные, так и одинарные кавычки.
ENT_IGNORE Молча отбрасывает некорректные кодовые последовательности вместо возврата пустой строки. Использование этого флага не рекомендуется, так как это » может внести уязвимости в ваш код.
ENT_SUBSTITUTE Заменяет некорреткные кодовые последовательности символом замены Юникода U+FFFD в случае использования UTF-8 и &#FFFD; при использовании другой кодировки, вместо возврата пустой строки.
ENT_DISALLOWED Заменяет неверные коды символов для заданного типа документа символом замены юникода U+FFFD (UTF-8) или &#FFFD; (при использовании другой кодировки) вместо того, чтобы оставлять все как есть. Это может быть полезно, например, для того, чтобы убедиться в формальной правильности XML-документов со встроенным внешним контентом.
ENT_HTML401 Обработка кода в соответствии с HTML 4.01.
ENT_XML1 Обработка кода в соответствии с XML 1.
ENT_XHTML Обработка кода в соответствии с XHTML.
ENT_HTML5 Обработка кода в соответствии с HTML 5.

encoding

Подобно htmlspecialchars(), функция htmlentities() принимает необязательный третий аргумент encoding, который задает кодировку, используемую при преобразовании. Если он не указан, то по умолчанию используется кодировка ISO-8859-1 в версиях PHP до 5.4.0 и UTF-8 - начиная с 5.4.0 и далее. Несмотря на то, что этот аргумент формально является необязательным, настоятельно рекомендуется указать правильное значение в вашем коде.

Поддерживаются следующие кодировки:

Поддерживаемые кодировки
Кодировка Псевдонимы Описание
ISO-8859-1 ISO8859-1 Западно-европейская Latin-1.
ISO-8859-5 ISO8859-5 Редкоиспользуемая кириллическая кодировка (Latin/Cyrillic).
ISO-8859-15 ISO8859-15 Западно-европейская Latin-9. Добавляет знак евро, французские и финские буквы к кодировке Latin-1(ISO-8859-1).
UTF-8   8-битная Unicode, совместимая с ASCII.
cp866 ibm866, 866 Кириллическая кодировка, применяемая в DOS.
cp1251 Windows-1251, win-1251, 1251 Кириллическая кодировка, применяемая в Windows.
cp1252 Windows-1252, 1252 Западно-европейская кодировка, применяемая в Windows.
KOI8-R koi8-ru, koi8r Русская кодировка.
BIG5 950 Традиционный китайский, применяется в основном на Тайване.
GB2312 936 Упрощенный китайский, стандартная национальная кодировка.
BIG5-HKSCS   Расширенная Big5, применяемая в Гонг-Конге.
Shift_JIS SJIS, SJIS-win, cp932, 932 Японская кодировка.
EUC-JP EUCJP, eucJP-win Японская кодировка.
MacRoman   Кодировка, используемая в Mac OS.
''   Пустая строка активирует режим определения кодировки из файла скрипта (Zend multibyte), default_charset и текущей локали (см. nl_langinfo() и setlocale()), в указанном порядке. Не рекомендуется к использованию.

Замечание: Остальные кодировки не поддерживаются, вместо них будет применена кодировка по умолчанию и сгенерировано предупреждение.

double_encode

При выключении параметра double_encode PHP не будет преобразовывать существующие html-сущности. По умолчанию преобразуется все без ограничений.

Возвращаемые значения

Возвращает преобразованную строку.

При наличии во входном параметре string недопустимой последовательности символов в заданной кодировке encoding будет возвращена пустая строка, если не установлены флаги ENT_IGNORE или ENT_SUBSTITUTE.

Список изменений

Версия Описание
5.4.0 Значение по умолчанию для параметра encoding было изменено на UTF-8.
5.4.0 The constants ENT_SUBSTITUTE, ENT_DISALLOWED, ENT_HTML401, ENT_XML1, ENT_XHTML и ENT_HTML5.
5.3.0 Добавлена константа ENT_IGNORE.
5.2.3 Добавлен параметр double_encode.
4.1.0 Добавлен параметр encoding.
4.0.3 Добавлен параметр flags.

Примеры

Пример #1 Пример использования htmlentities()

<?php
$str 
"A 'quote' is <b>bold</b>";

// выводит: A 'quote' is &lt;b&gt;bold&lt;/b&gt;
echo htmlentities($str);

// выводит: A &#039;quote&#039; is &lt;b&gt;bold&lt;/b&gt;
echo htmlentities($strENT_QUOTES);
?>

Пример #2 Использование ENT_IGNORE

<?php
$str 
"\x8F!!!";

// Выводит пустую строку
echo htmlentities($strENT_QUOTES"UTF-8");

// Выводит "!!!"
echo htmlentities($strENT_QUOTES ENT_IGNORE"UTF-8");
?>

Смотрите также

  • html_entity_decode() - Преобразует все HTML-сущности в соответствующие символы
  • get_html_translation_table() - Возвращает таблицу преобразований, используемую функциями htmlspecialchars и htmlentities
  • htmlspecialchars() - Преобразует специальные символы в HTML-сущности
  • nl2br() - Вставляет HTML-код разрыва строки перед каждым переводом строки
  • urlencode() - URL-кодирование строки

Коментарии

Автор:
Note that you'll have use htmlentities() before any other function who'll edit text like nl2br().

If you use nl2br() first, the htmlentities() function will change < br > to &lt;br&gt;.
2003-01-05 18:07:48
http://php5.kiev.ua/manual/ru/function.htmlentities.html
This fuction is particularly useful against XSS (cross-site-scripting-). XSS makes use of holes in code, whether it be in Javascript or PHP. XSS often, if not always, uses HTML entities to do its evil deeds, so this function in co-operation with your scripts (particularly search or submitting scripts) is a very useful tool in combatting "H4X0rz".
2004-04-29 17:29:15
http://php5.kiev.ua/manual/ru/function.htmlentities.html
If you are building a loadvars page for Flash and have problems with special chars such as " & ", " ' " etc, you should escape them for flash:

Try trace(escape("&")); in flash' actionscript to see the escape code for &;

% = %25
& = %26
' = %27

<?php
function flashentities($string){
return 
str_replace(array("&","'"),array("%26","%27"),$string);
}
?>

Those are the two that concerned me. YMMV.
2006-11-06 12:41:36
http://php5.kiev.ua/manual/ru/function.htmlentities.html
I've seen lots of functions to convert all the entities, but I needed to do a fulltext search in a db field that had named entities instead of numeric entities (edited by tinymce), so I searched the tinymce source and found a string with the value->entity mapping. So, i wrote the following function to encode the user's query with named entities.

The string I used is different of the original, because i didn't want to convert ' or ". The string is too long, so I had to cut it. To get the original check TinyMCE source and search for nbsp or other entity ;)

<?php

$entities_unmatched 
explode(',''160,nbsp,161,iexcl,162,cent, [...] ');
$even 1;
foreach(
$entities_unmatched as $c) {
    if(
$even) {
       
$ord $c;
    } else {
       
$entities_table[$ord] = $c;
    }
   
$even $even;
}

function 
encode_named_entities($str) {
    global 
$entities_table;
   
   
$encoded_str '';
    for(
$i 0$i strlen($str); $i++) {
       
$ent = @$entities_table[ord($str{$i})];
        if(
$ent) {
           
$encoded_str .= "&$ent;";
        } else {
           
$encoded_str .= $str{$i};
        }
    }
    return 
$encoded_str;
}

?>
2007-03-07 06:41:47
http://php5.kiev.ua/manual/ru/function.htmlentities.html
Автор:
Trouble when using files with different charset?

htmlentities and html_entity_decode can be used to translate between charset! 

Sample function:

<?php
function utf2latin($text) {
   
$text=htmlentities($text,ENT_COMPAT,'UTF-8');
   return 
html_entity_decode($text,ENT_COMPAT,'ISO-8859-1');
}
?>
2008-04-15 12:15:05
http://php5.kiev.ua/manual/ru/function.htmlentities.html
Автор:
Note that as of 5.2.5 it appears that if the input string contains a character that is not valid for the output encoding you've specified, then this function returns null.

You might expect it to just strip the invalid char, but it doesn't.

You can strip the chars yourself like so:

iconv('utf-8','utf-8',$str);

You can combine that with htmlentities also:

$str = htmlentities(iconv('UTF-8', 'UTF-8//IGNORE', $str, ENT_QUOTES, 'UTF-8');

Should give you a string with htmlentities encoded to utf-8, and any unsupported chars stripped.
2008-10-16 22:14:14
http://php5.kiev.ua/manual/ru/function.htmlentities.html
The following will make a string completely safe for XML:

<?php
function philsXMLClean($strin) {
       
$strout null;

        for (
$i 0$i strlen($strin); $i++) {
               
$ord ord($strin[$i]);

                if ((
$ord && $ord 32) || ($ord >= 127)) {
                       
$strout .= "&amp;#{$ord};";
                }
                else {
                        switch (
$strin[$i]) {
                                case 
'<':
                                       
$strout .= '&lt;';
                                        break;
                                case 
'>':
                                       
$strout .= '&gt;';
                                        break;
                                case 
'&':
                                       
$strout .= '&amp;';
                                        break;
                                case 
'"':
                                       
$strout .= '&quot;';
                                        break;
                                default:
                                       
$strout .= $strin[$i];
                        }
                }
        }

        return 
$strout;
}
?>
2010-04-08 11:34:19
http://php5.kiev.ua/manual/ru/function.htmlentities.html
Автор:
An important note below about using this function to secure your application against Cross Site Scripting (XSS) vulnerabilities.

When printing user input in an attribute of an HTML tag, the default configuration of htmlEntities() doesn't protect you against XSS, when using single quotes to define the border of the tag's attribute-value. XSS is then possible by injecting a single quote:

<?php
$_GET
['a'] = "#000' onload='alert(document.cookie)";
?>

XSS possible (insecure):

<?php
$href 
htmlEntities($_GET['a']);
print 
"<body bgcolor='$href'>"# results in: <body bgcolor='#000' onload='alert(document.cookie)'>
?>

Use the 'ENT_QUOTES' quote style option, to ensure no XSS is possible and your application is secure:

<?php
$href 
htmlEntities($_GET['a'], ENT_QUOTES);
print 
"<body bgcolor='$href'>"# results in: <body bgcolor='#000&#039; onload=&#039;alert(document.cookie)'>
?>

The 'ENT_QUOTES' option doesn't protect you against javascript evaluation in certain tag's attributes, like the 'href' attribute of the 'a' tag. When clicked on the link below, the given JavaScript will get executed:

<?php
$_GET
['a'] = 'javascript:alert(document.cookie)';
$href htmlEntities($_GET['a'], ENT_QUOTES);
print 
"<a href='$href'>link</a>"# results in: <a href='javascript:alert(document.cookie)'>link</a>
?>
2010-09-13 15:18:04
http://php5.kiev.ua/manual/ru/function.htmlentities.html
I use this function to encode all the xml entities and also all the &something; that are not defined in xml like &trade;
You can also decode what you encode with my decode function.
My function works a little like the htmlentities.
You can also add other string to the array if you want to exclude them from the encoding.

<?php
function xml_entity_decode($text$charset 'Windows-1252'){
   
// Double decode, so if the value was &amp;trade; it will become Trademark
   
$text html_entity_decode($textENT_COMPAT$charset);
   
$text html_entity_decode($textENT_COMPAT$charset);
    return 
$text;
}

function 
xml_entities($text$charset 'Windows-1252'){
     
// Debug and Test
    // $text = "test &amp; &trade; &amp;trade; abc &reg; &amp;reg; &#45;";
   
    // First we encode html characters that are also invalid in xml
   
$text htmlentities($textENT_COMPAT$charsetfalse);
   
   
// XML character entity array from Wiki
    // Note: &apos; is useless in UTF-8 or in UTF-16
   
$arr_xml_special_char = array("&quot;","&amp;","&apos;","&lt;","&gt;");
   
   
// Building the regex string to exclude all strings with xml special char
   
$arr_xml_special_char_regex "(?";
    foreach(
$arr_xml_special_char as $key => $value){
       
$arr_xml_special_char_regex .= "(?!$value)";
    }
   
$arr_xml_special_char_regex .= ")";
   
   
// Scan the array for &something_not_xml; syntax
   
$pattern "/$arr_xml_special_char_regex&([a-zA-Z0-9]+;)/";
   
   
// Replace the &something_not_xml; with &amp;something_not_xml;
   
$replacement '&amp;${1}';
    return 
preg_replace($pattern$replacement$text);
}
?>
2010-09-17 16:14:24
http://php5.kiev.ua/manual/ru/function.htmlentities.html
A pointer to function.mb-convert-encoding if your intention is to translate *all* characters in a charset to their corresponding HTML entities, not just named characters. Non-named characters will be replaced with HTML numeric encoding. eg:

$text = mb_convert_encoding($text, 'HTML-ENTITIES', "UTF-8");
2010-09-29 19:11:44
http://php5.kiev.ua/manual/ru/function.htmlentities.html
A useful little function to convert the symbols in the different inputs.
<?php
function ConvertSimbols($var$ConvertQuotes 0) {
if (
$ConvertQuotes 0) {
$var htmlentities($varENT_NOQUOTES'UTF-8');
$var str_replace('\"'''$var);
$var str_replace("\'"''$var);
} else {
$var htmlentities($varENT_QUOTES'UTF-8');
}
return 
$var;
}
?>

Usage with quotes for example message:

$message = ConvertSimbols($message);

Usage without quotes for example link:

$link = ConvertSimbols($link, 1);
2011-02-24 15:11:23
http://php5.kiev.ua/manual/ru/function.htmlentities.html
htmlentities seems to have changed at some point between version 5.1.6 and 5.3.3, such that it now returns an empty string for anything containing a pound sign:

$ php -v
PHP 5.1.6 (cli) (built: May 22 2008 09:08:44)
$ php -r "echo htmlentities('£hello', null, 'utf-8');"
&pound;hello
$

$ php -v
PHP 5.3.3 (cli) (built: Aug 19 2010 12:07:49)
$ php -r "echo htmlentities('£hello', null, 'utf-8');"
$

(Returns an empty string the second time)

Just a heads up.
2011-04-15 11:14:01
http://php5.kiev.ua/manual/ru/function.htmlentities.html
Автор:
When putting values inside comment tags <!-- --> you should replace -- with &#45;&#45; too, as this would end your tag and show the rest of the comment.
2011-06-06 05:09:20
http://php5.kiev.ua/manual/ru/function.htmlentities.html
I'm glad 5.4 has xml support, but many of us are working with older installations, some of us still have to use PHP4. If you're like me you've been frustrated with trying to use htmlentites/htmlspecial chars with xml output. I was hoping to find an option to force numeric encoding, lacking that, I have written my own xmlencode function, which I now offer:

usage: 

$string xmlencode( $string )

it will use htmlspecialchars for the valid xml entities amp, quote, lt, gt, (apos) and return the numeric entity for all other non alpha-numeric characters.

-------------------------------------------

<?php 
if( !function_exists'xmlentities' ) ) {
    function 
xmlentities$string ) {
       
$not_in_list "A-Z0-9a-z\s_-";
        return 
preg_replace_callback"/[^{$not_in_list}]/" 'get_xml_entity_at_index_0' $string );
    }
    function 
get_xml_entity_at_index_0$CHAR ) {
        if( !
is_string$CHAR[0] ) || ( strlen$CHAR[0] ) > ) ) {
            die( 
"function: 'get_xml_entity_at_index_0' requires data type: 'char' (single character). '{$CHAR[0]}' does not match this type." );
        }
        switch( 
$CHAR[0] ) {
            case 
"'":    case '"':    case '&':    case '<':    case '>':
                return 
htmlspecialchars$CHAR[0], ENT_QUOTES );    break;
            default:
                return 
numeric_entity_4_char($CHAR[0]);                break;
        }       
    }
    function 
numeric_entity_4_char$char ) {
        return 
"&#".str_pad(ord($char), 3'0'STR_PAD_LEFT).";";
    }   
}
?>
2011-11-16 22:59:36
http://php5.kiev.ua/manual/ru/function.htmlentities.html
Hi there,

after several and several tests, I figured out that dot:

- htmlentities() function remove characters like "à","è",etc when you specify a flag and a charset

- htmlentities() function DOES NOT remove characters like those above when you DO NOT specify anything

So, let's assume that..

<?php

$str 
"Hèèèllooo";

$res_1 htmlentities($strENT_QUOTES"UTF-8");
$res_2 htmlentities($str);

echo 
var_dump($res_1); // Result: string '' (length=0)
echo var_dump($res_2); // string 'H&egrave;&egrave;&egrave;llooo' (length=30)

?>

I used this for a textarea content for comments. Anyway, note that using the "$res_2" form the function will leave unconverted single/double quotes. At this point you should use str_replace() function to perform the characters but be careful because..

<?php

$str 
"'Hèèèllooo'";

$res_2 str_replace("'","&#039;",$str);
$res_2 htmlentities($str);
echo 
var_dump($res_2); // string '&amp;#039;H&egrave;&egrave;&egrave;llooo&amp;#039;'

$res_3 htmlentities($str);
$res_3 str_replace("'","&#039;",$res_3);
echo 
var_dump($res_3); // string '&#039;H&egrave;&egrave;&egrave;llooo&#039;' --> Nice
?>

Hope it will helps you.

Regards,
W.D.
2011-12-19 04:20:47
http://php5.kiev.ua/manual/ru/function.htmlentities.html
Автор:
html entities does not encode all unicode characters. It encodes what it can [all of latin1], and the others slip through. &#1033; is the nasty I use. I have searched for a function which encodes everything, but in the end I wrote this. This is as simple as I can get it. Consult an ansii table to custom include/omit chars you want/don't. I'm sure it's not that fast.

// Unicode-proof htmlentities. 
// Returns 'normal' chars as chars and weirdos as numeric html entites.
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;
}
2012-03-20 14:46:04
http://php5.kiev.ua/manual/ru/function.htmlentities.html
For those Spanish (and not only) folks, that want their national letters back after htmlentities :)

<?php
protected function _decodeAccented($encodedValue$options = array()) {
   
$options += array(
       
'quote'     => ENT_NOQUOTES,
       
'encoding'  => 'UTF-8',
    );
    return 
preg_replace_callback(
       
'/&\w(acute|uml|tilde);/',
       
create_function(
           
'$m',
           
'return html_entity_decode($m[0], ' $options['quote'] . ', "' .
           
$options['encoding'] . '");'
       
),
       
$encodedValue
   
);
}
?>
2012-04-09 17:15:46
http://php5.kiev.ua/manual/ru/function.htmlentities.html
Автор:
The flag ENT_HTML5 also strips newline chars like \n with htmlentities while htmlspecialchars is not affected by that.

If you want to use nl2br on that string afterwards you might end up searching the problem like i did. This does not apply to other flags like e.g. ENT_XHTML which confused me.

Tested this with PHP 5.4 / 5.5 / 5.6-dev with same results, so it seems that this is an intended "feature".
2014-01-05 19:18:27
http://php5.kiev.ua/manual/ru/function.htmlentities.html
This function throws a warning on bad input even if ENT_SUBSTITUTE is set, so be prepared for this.
2017-04-10 04:10:32
http://php5.kiev.ua/manual/ru/function.htmlentities.html
Автор:
There is a feature when writing to XML using an AJAX call to PHP that rarely is mentioned. I struggled for many hours using htmlentities() because what was getting written to my XML document was not as expected. I naturally assumed that I should be converting my strings before writing them to XML to adhere to XML rules on illegal characters. To my surprise, when converting with htmlentities() or htmlspecialchars() and then writing to an XML file, the resulting ampersands get converted afterwards! Consider the following example:

<?php
$str 
"<b>I am cool</b>" ;
$str htmlentities($str) ;
?>

When you append $str to an XML element and save() the document, you would expect the XML document's source code to look something like this:

<ele>&lt;b&gt;I am cool&lt;/b&gt;</ele>

But that is not what happens. The resulting ampersands get converted by PHP automatically to &amp; and your source code ends up looking like this:

<ele>&amp;lt;b&amp;gt;I am cool&amp;lt;/b&amp;gt;</ele>

As you can see, this creates problems when trying to output the XML data back to HTML. It is important to remember that when writing to XML this way, special characters like ">" and "<"; PHP converts them automatically and there becomes no need to use htmlentities() in certain cases. I assume this feature is in place to aid with passing data through header queries, to avoid reserved characters conflicting with others in a header query (e.g. & or =). Now I understand this may not be the case with older versions of PHP and that this might be a feature of my version (PHP version 5.6.32). With older versions, I assume using htmlentities() or htmlspecialchars() is a must, as stated with previous notes here. Also I use the charset UTF-8 in my HTML and XML and am not sure if this also effects the results I get.

Anyway, I struggled for many hours with using htmlentities() to convert strings for XML writing and saving, when all I had to do was simply not use the function and let PHP convert my strings for me. I hope this helps because I would think I am not the only one who has struggled with this situation.
2018-06-01 16:25:42
http://php5.kiev.ua/manual/ru/function.htmlentities.html
<?php

/**
 * 将中文转为Html实体
 * Convert Chinese in HTML to entity
 * Author QiangGe
 * Mail 2962051004@qq.com
 *
*/

$str = <<<EOT
你好 world
EOT;

function 
ChineseToEntity($str) {
 return 
preg_replace_callback(
       
'/[\x{4e00}-\x{9fa5}]/u'// utf-8 
        // '/[\x7f-\xff]+/', // if gb2312
       
function ($matches) {
           
$json json_encode(array($matches[0]));
           
preg_match('/\[\"(.*)\"\]/'$json$arr);
           
/*
             * 通过json_encode函数将中文转为unicode
             * 然后用正则取出unicode
             * Turn the Chinese into Unicode through the json_encode function, then extract Unicode from regular.
             * I think this idea is seamless.
            */
           
return '&#x'str_replace('\\u'''$arr[1]). ';';
        }, 
$str
   
);
}

echo 
ChineseToEntity($str);
// &#x4f60;&#x597d; world
2018-10-03 19:09:14
http://php5.kiev.ua/manual/ru/function.htmlentities.html
The answer above is not correct for multiple languages like France
I had correct it
function xml_entities($strIn)
    {
        if (is_numeric($strIn)) {
            return $strIn;
        }
        $strOut = null;

        $arrStr = mb_str_split($strIn);
        foreach ($arrStr as $char) {
            $ord = mb_ord($char);

            if (($ord > 0 && $ord < 32) || ($ord >= 127)) {
                $strOut .= "&amp;#{$ord};";
            }
            else {
                switch ($char) {
                    case '<':
                        $strOut .= '&lt;';
                        break;
                    case '>':
                        $strOut .= '&gt;';
                        break;
                    case '&':
                        $strOut .= '&amp;';
                        break;
                    case '"':
                        $strOut .= '&quot;';
                        break;
                    default:
                        $strOut .= $char;
                }
            }
        }

        return $strOut;
    }
2022-05-04 12:31:05
http://php5.kiev.ua/manual/ru/function.htmlentities.html

    Поддержать сайт на родительском проекте КГБ