HTML characters and tags in PHP _php tutorial

Source: Internet
Author: User
Htmlspecialchars (), Htmlspecialchars_decode ()

Definition and usage

The Htmlspecialchars () function converts some pre-defined characters to HTML entities.

The predefined characters are:
& (and number) becomes &
"(double quotes) becomes"
' (single quote) become '
< (less than) becomes <
> (greater than) becomes >
Grammar
Htmlspecialchars (String,quotestyle,character-set)
Parameter description
string is required. Specifies the string to convert.
Quotestyle is optional. Specifies how to encode single and double quotation marks.
Ent_compat-Default. Encodes only double quotes.
Ent_quotes-encodes double and single quotation marks.
Ent_noquotes-do not encode any quotation marks.

Character-set is optional. A string value that specifies the character set to use.
Iso-8859-1-Default. Western europe.
Iso-8859-15-Western Europe (added Euro symbol and French, Finnish alphabet).
UTF-8-ASCII-compatible multi-byte 8-bit Unicode
Cp866-dos dedicated Cyrillic Character Set
Cp1251-windows dedicated Cyrillic Character Set
Cp1252-windows Special Western European character set
Koi8-r-Russian
GB2312-Simplified Chinese, national standard character set
BIG5-Traditional Chinese
Big5-hkscs-big5 Hong Kong expansion
Shift_JIS-Japanese
EUC-JP-Japanese
Hints and Notes
Hint: A character set that cannot be recognized is ignored and replaced by Iso-8859-1.
Example
PHP code


$str = "John & ' Adams '";
Echo Htmlspecialchars ($str, Ent_compat);
echo "
";
Echo Htmlspecialchars ($str, ent_quotes);
echo "
";
Echo Htmlspecialchars ($str, ent_noquotes);
?>

Browser output:

John & ' Adams '
John & ' Adams '
John & ' Adams '

If you view the source code in a browser, you will see these HTML:
PHP code


John & ' Adams '

John & ' Adams '

John & ' Adams '


Definition and usage
The Htmlspecialchars_decode () function converts some pre-defined HTML entities to characters.

The HTML entities that will be decoded are:
& Become & (and No.)
"Become" (double quotes)
' Become ' (single quote)
< becomes < (less than)
> Become > (Greater than)
Grammar
Htmlspecialchars_decode (String,quotestyle)

Parameter description
string is required. Specifies the string to decode.
Quotestyle is optional. Specifies how to decode single and double quotation marks.
Ent_compat-Default. Decodes only double quotes.
Ent_quotes-decodes double and single quotes.
Ent_noquotes-Do not decode any quotation marks.
Example
PHP code
$str = "John & ' Adams '";
echo Htmlspecialchars_decode ($STR);
echo "
";
Echo Htmlspecialchars_decode ($str, ent_quotes);
echo "
";
Echo Htmlspecialchars_decode ($str, ent_noquotes);
?>

Browser output:

John & ' Adams '
John & ' Adams '
John & ' Adams '

If you view the source code in a browser, you will see these HTML:
PHP code


John & ' Adams '

John & ' Adams '

John & ' Adams '

http://www.bkjia.com/PHPjc/478675.html www.bkjia.com true http://www.bkjia.com/PHPjc/478675.html techarticle The htmlspecialchars (), Htmlspecialchars_decode () Definition and usage htmlspecialchars () function converts some of the predefined characters to HTML entities. The predefined characters are: (and number) into ...

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.