دالة PHP htmlentities ()
مثال
تحويل بعض الأحرف إلى كيانات HTML:
<?php
$str = '<a href="https://www.w3schools.com">Go to
w3schools.com</a>';
echo htmlentities($str);
?>
سيكون ناتج HTML للشفرة أعلاه (عرض المصدر):
<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 للشفرة أعلاه (عرض المصدر):
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 للشفرة أعلاه (عرض المصدر):
<!DOCTYPE html>
<html>
<body>
My name is Øyvind Åsane. I'm Norwegian.
</body>
</html>
سيكون إخراج المتصفح للشفرة أعلاه هو:
My name is Øyvind Åsane. I'm Norwegian.
❮ مرجع سلسلة PHP