Jump to: navigation, search

Php htmlspecialchars

From w3cyberlearnings

Contents

PHP function htmlspecialchars

This function converts predefined characters to HTML entities.

Syntax htmlspecialchars

  • string: string input
  • quotestyle: How to encode double and single quotes
    • ENT_COMPAT - Default, encodes double quotes only
    • ENT_QUOTE - Encodes single and double quotes
    • ENT_NOTQUOTES - Not encodes double and single quotes.
    • ENT_IGNORE - Silently discard invalid code unit sequences instead of returning an empty string. Using this flag is discouraged as it ยป may have security implications.
    • ENT_SUBSTITUTE - Replace invalid code unit sequences with a Unicode Replacement Character U+FFFD (UTF-8) or &#FFFD; (otherwise) instead of returning an empty string.
    • ENT_DISALLOWED - Replace invalid code points for the given document type with a Unicode Replacement Character U+FFFD (UTF-8) or &#FFFD; (otherwise) instead of leaving them as is. This may be useful, for instance, to ensure the well-formedness of XML documents with embedded external content.
    • ENT_HTML401 - Handle code as HTML 4.01.
    • ENT_XML1 - Handle code as XML 1.
    • ENT_XHTML - Handle code as XHTML.
    • ENT_HTML5 - Handle code as HTML 5.
  • character-set:
    • ISO-8859-1 - Default. Western European
    • ISO-8859-15 - Western European (adds the Euro sign + French and Finnish letters missing in ISO-8859-1)
    • UTF-8 - ASCII compatible multi-byte 8-bit Unicode
    • cp866 - DOS-specific Cyrillic charset
    • cp1251 - Windows-specific Cyrillic charset
    • cp1252 - Windows specific charset for Western European
    • KOI8-R - Russian
    • BIG5 - Traditional Chinese, mainly used in Taiwan
    • GB2312 - Simplified Chinese, national standard character set
    • BIG5-HKSCS - Big5 with Hong Kong extensions
    • Shift_JIS - Japanese
    • EUC-JP - Japanese
htmlspecialchars(string,quotestyle,character-set);

Predefine characters

Predefine special characters.png

Example 1

<?php

$numberfor ="a + b > 30";
echo htmlspecialchars($numberfor);
?>

Output

a + b > 30 
// display as HTML view source
a + b &gt; 30

Example 2

<?php
$new = htmlspecialchars("<a href='w3cyberlearnings.com'>Learning</a>", ENT_QUOTES);
echo $new; 
?>

Output

<a href='w3cyberlearnings.com'>Learning</a>
// dispaly as HTML view source
&lt;a href=&#039;w3cyberlearnings.com&#039;&gt;Learning&lt;/a&gt;

Related Functions


Navigation
Web
SQL
MISC
References