PHP htmlentities()ฟังก์ชัน
ตัวอย่าง
แปลงอักขระบางตัวเป็นเอนทิตี HTML:
<?php
$str = '<a href="https://www.w3schools.com">Go to
w3schools.com</a>';
echo htmlentities($str);
?>
เอาต์พุต HTML ของโค้ดด้านบนจะเป็น (View Source):
<a href="https://www.w3schools.com">Go to w3schools.com</a>
ผลลัพธ์ของเบราว์เซอร์ของโค้ดด้านบนจะเป็น:
<a href="https://www.w3schools.com">Go to w3schools.com</a>
ความหมายและการใช้งาน
ฟังก์ชัน htmlentities() แปลงอักขระเป็นเอนทิตี HTML
เคล็ดลับ:ในการแปลงเอนทิตี HTML กลับเป็นอักขระ ให้ใช้ ฟังก์ชันhtml_entity_decode()
เคล็ดลับ:ใช้ ฟังก์ชัน get_html_translation_table()เพื่อส่งคืนตารางการแปลที่ใช้โดย htmlentities()
ไวยากรณ์
htmlentities(string,flags,character-set,double_encode)
ค่าพารามิเตอร์
Parameter | Description |
---|---|
string | Required. Specifies the string to convert |
flags | Optional. Specifies how to handle quotes, invalid encoding and the used document type. The available quote styles are:
Invalid encoding:
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. |
double_encode | Optional. A boolean value that specifies whether to encode existing html entities or not.
|
รายละเอียดทางเทคนิค
คืนมูลค่า: | ส่งกลับสตริงที่แปลงแล้ว อย่างไรก็ตาม หาก พารามิเตอร์ สตริง มีการเข้ารหัสที่ไม่ถูกต้อง พารามิเตอร์จะส่งคืนสตริงว่าง เว้นแต่จะตั้งค่าสถานะ ENT_IGNORE หรือ ENT_SUBSTITUTE |
---|---|
รุ่น PHP: | 4+ |
บันทึกการเปลี่ยนแปลง: | PHP 5.6 - เปลี่ยนค่าเริ่มต้นสำหรับ พารามิเตอร์ ชุดอักขระ
เป็นค่าของชุดอักขระเริ่มต้น (ในการกำหนดค่า) PHP 5.4 - เปลี่ยนค่าเริ่มต้นสำหรับ พารามิเตอร์ ชุดอักขระเป็น UTF-8 PHP 5.4 - เพิ่ม ENT_SUBSTITUTE, ENT_DISALLOWED, ENT_HTML401, ENT_HTML5, ENT_XML1 และ ENT_XHTML PHP 5.3 - เพิ่มค่าคงที่ ENT_IGNORE PHP 5.2.3 - เพิ่มพารามิเตอร์double_encode PHP 4.1 - เพิ่ม พารามิเตอร์ชุดอักขระ |
ตัวอย่างเพิ่มเติม
ตัวอย่าง
แปลงอักขระบางตัวเป็นเอนทิตี HTML:
<?php
$str = "Albert Einstein said: 'E=MC²'";
echo htmlentities($str, ENT_COMPAT); // Will only convert double quotes
echo "<br>";
echo htmlentities($str, ENT_QUOTES); // Converts double and single quotes
echo "<br>";
echo htmlentities($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 htmlentities($str, ENT_QUOTES, "UTF-8"); // Will only convert double quotes (not single quotes), and uses the character-set Western European
?>
เอาต์พุต HTML ของโค้ดด้านบนจะเป็น (View Source):
<!DOCTYPE html>
<html>
<body>
My name is Øyvind Åsane. I'm Norwegian.
</body>
</html>
ผลลัพธ์ของเบราว์เซอร์ของโค้ดด้านบนจะเป็น:
My name is Øyvind Åsane. I'm Norwegian.
❮ การอ้างอิงสตริง PHP