Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> PHP

ฟังก์ชัน htmlentities() ใน PHP

คำอธิบาย

ฟังก์ชัน htmlentities() ใช้แปลงอักขระทุกตัวที่มี HTML entity เทียบเท่า ให้กลายเป็น entity นั้น แตกต่างจาก htmlspecialchars() ที่แปลงเฉพาะอักขระพิเศษ 5 ตัว ฟังก์ชันนี้แปลงอักขระทุกตัวที่มี entity รวมถึงอักขระอักษรต่างประเทศ

รูปแบบการใช้งาน (Syntax)

htmlentities(string $string, int $flags = ENT_COMPAT | ENT_HTML401, string $encoding = "UTF-8", bool $double_encode = true): string

พารามิเตอร์

เหมือน htmlspecialchars() ทุกประการ

ค่าที่คืนกลับ (Return Value)

คืนค่าสตริงที่แปลงแล้ว

ตัวอย่างการใช้งาน

<?php
$s = "Cricket is a 'religion' in India";
echo htmlentities($s);
echo htmlentities($s, ENT_QUOTES);
?>

ผลลัพธ์

Cricket is a 'religion' in <b>India</b>Cricket is a 'religion' in <b>India</b>

ความแตกต่างจาก htmlspecialchars(): htmlentities() แปลงอักขระทุกตัวที่มี entity (เช่น © → &copy;, é → &eacute;) ส่วน htmlspecialchars() แปลงเฉพาะ 5 อักขระพื้นฐาน