สำหรับฟังก์ชัน htmlspecialchars_decode() เป็นฟังก์ชันของ PHP ที่เราสามารถเปลี่ยน predefined characters เป็น HTML entities ได้ซึ่งจะทำให้ข้อมูลที่เป็น string ธรรมดากลายเป็นโค้ด HTML ได้
สำหรับ predefined characters มีดังนี้
& (ampersand) becomes &
" (double quote) becomes "
' (single quote) becomes '
< (less than) becomes <
> (greater than) becomes >
รูปแบบการใช้
htmlspecialchars(string , quotestyle , character-set)
string คือ สตริงที่ต้องการเปลี่ยน
quotestyle คือ
ENT_COMPAT - (Default) Decodes แค่ double quotes
ENT_QUOTES - Decodes ทั้ง double and single quotes
ENT_NOQUOTES - ไม่ decode any quotes
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
ตัวอย่าง
<?php
$str = "mind & php";
$str2= htmlspecialchars($str, ENT_QUOTES);
echo $str;
echo "<hr>";
echo $str2;
?>
ผลที่ได้
mind & php<hr>mind & php
จะเห็นได้ว่า string ที่ถูกใช่ฟังก์ชัน htmlspecialchars_decode นั้น string ที่เป็น predefined characters จะถูกเปลี่ยนเป็น HTML entities นั้นเอง