PHP html_entity_decode()ฟังก์ชั่น
ตัวอย่าง
แปลงเอนทิตี HTML เป็นอักขระ:
<?php
$str = '<a href="https://www.w3schools.com">w3schools.com</a>';
echo html_entity_decode($str);
?>
เอาต์พุต HTML ของโค้ดด้านบนจะเป็น (View Source):
<a href="https://www.w3schools.com">w3schools.com</a>
ผลลัพธ์ของเบราว์เซอร์ของโค้ดด้านบนจะเป็น:
ความหมายและการใช้งาน
ฟังก์ชัน html_entity_decode() แปลงเอนทิตี HTML เป็นอักขระ
ฟังก์ชัน html_entity_decode() ตรงกันข้ามกับ htmlentities( )
ไวยากรณ์
html_entity_decode(string,flags,character-set)
ค่าพารามิเตอร์
Parameter | Description |
---|---|
string | Required. Specifies the string to decode |
flags | Optional. Specifies how to handle quotes and which document type to use. The available quote styles are:
Additional flags for specifying the used doctype:
|
character-set | Optional. A string that specifies which character-set to use. Allowed values are:
Note: Unrecognized character-sets will be ignored and replaced by ISO-8859-1 in versions prior to PHP 5.4. As of PHP 5.4, it will be ignored an replaced by UTF-8. |
รายละเอียดทางเทคนิค
คืนมูลค่า: | ส่งกลับสตริงที่แปลงแล้ว |
---|---|
รุ่น PHP: | 4.3.0+ |
บันทึกการเปลี่ยนแปลง: | PHP 5.6 - เปลี่ยนค่าเริ่มต้นสำหรับ พารามิเตอร์ ชุดอักขระ
เป็นค่าของชุดอักขระเริ่มต้น (ในการกำหนดค่า) PHP 5.4 - เปลี่ยนค่าเริ่มต้นสำหรับ พารามิเตอร์ ชุดอักขระเป็น UTF-8 PHP 5.4 - เพิ่ม ENT_HTML401, ENT_HTML5, ENT_XML1 และ ENT_XHTML PHP 5.0 - เพิ่มการรองรับการเข้ารหัสแบบหลายไบต์ |
ตัวอย่างเพิ่มเติม
ตัวอย่าง
แปลงเอนทิตี HTML บางตัวเป็นอักขระ:
<?php
$str = "Albert Einstein said: 'E=MC²'";
echo
html_entity_decode($str, ENT_COMPAT); // Will only convert double quotes
echo "<br>";
echo html_entity_decode($str, ENT_QUOTES); // Converts double
and single quotes
echo "<br>";
echo html_entity_decode($str, ENT_NOQUOTES); // Does not
convert any quotes
?>
เอาต์พุต HTML ของโค้ดด้านบนจะเป็น (View Source):
Albert Einstein said: 'E=MC²'<br>
Albert Einstein said: 'E=MC²'<br>
Albert Einstein said: 'E=MC²'
ผลลัพธ์ของเบราว์เซอร์ของโค้ดด้านบนจะเป็น:
Albert Einstein said: 'E=MC²'
Albert Einstein said: 'E=MC²'
Albert
Einstein said: 'E=MC²'
ตัวอย่าง
แปลงเอนทิตี HTML บางรายการเป็นอักขระ โดยใช้ชุดอักขระยุโรปตะวันตก:
<?php
$str = "My name is Øyvind Åsane. I'm Norwegian.";
echo html_entity_decode($str, ENT_QUOTES, "UTF-8");
?>
เอาต์พุต HTML ของโค้ดด้านบนจะเป็น (View Source):
My name is Øyvind Åsane. I'm Norwegian.
ผลลัพธ์ของเบราว์เซอร์ของโค้ดด้านบนจะเป็น:
My name is Øyvind Åsane. I'm Norwegian.
❮ การอ้างอิงสตริง PHP