توابع html_entity_decode() در PHP

مثال

HTML entity‌های پیش‌تعریف شده "<" (کوچکتر) و ">" (بزرگتر) به کاراکترها تبدیل می‌شوند:

<?php
$str = "این یک متن تیره است. <b>تیره</b>.";
echo htmlspecialchars_decode($str);
?>

The HTML output of the above code is as follows (view source code):

<!DOCTYPE html>
<html>
<body>
این یک متن تیره است. <b>تیره</b>.
</body>
</html>

Browser output of the above code:

این یک متن تیره است.

تعریف و نحوه‌ی استفاده

htmlspecialchars_decode() عملکردی تبدیل HTML entity‌های پیش‌تعریف شده به کاراکترها را انجام می‌دهد.

HTML entity‌هایی که پردازش می‌شوند:

  • & به & (و) تبدیل می‌شود
  • " به " (نقل قول دوگانه) تبدیل می‌شود
  • ' به ' (نقل قول یکگانه) تبدیل می‌شود
  • < به < (کوچکتر) تبدیل می‌شود
  • > به > (بزرگتر) تبدیل می‌شود

htmlspecialchars_decode() عملکردی برعکس htmlspecialchars() است.

نحوه‌ی استفاده

htmlspecialchars_decode(string,flags)
پارامتر توضیح
string ضروری. تعیین می‌کند که چه دکمه‌ای باید پردازش شود.
flags

اختیاری. تعیین می‌کند چگونه نقل قول‌ها پردازش شوند و چه نوع مستندی استفاده شود.

نوع‌های نقل قول موجود:

  • ENT_COMPAT - پیش‌فرض. تنها نقل قول‌های دوگانه را پردازش کند.
  • ENT_QUOTES - نقل قول‌های دوگانه و یکگانه را پردازش کند.
  • ENT_NOQUOTES - هیچ نقل قولی را نپردازد.

محدوده‌ای که باید از نوع مستند استفاده شود:

  • ENT_HTML401 - پیش‌فرض. به عنوان کد HTML 4.01 پردازش می‌شود.
  • ENT_HTML5 - به عنوان کد HTML 5 پردازش می‌شود.
  • ENT_XML1 - به عنوان کد XML 1 پردازش می‌شود.
  • ENT_XHTML - به عنوان کد XHTML پردازش می‌شود.

Technical Details

Return Value: Return the converted string.
PHP Version: 5.1.0+
Update Log:

In PHP 5.4, additional flags were added to specify the document type to be used:

  • ENT_HTML401
  • ENT_HTML5
  • ENT_XML1
  • ENT_XHTML

More Examples

Example 1

Convert predefined HTML entities to characters:

<?php
$str = "Bill & 'Steve'";
echo htmlspecialchars_decode($str, ENT_COMPAT); // Convert only double quotes
echo "<br>";
echo htmlspecialchars_decode($str, ENT_QUOTES); // Convert double and single quotes
echo "<br>";
echo htmlspecialchars_decode($str, ENT_NOQUOTES); // Do not convert any quotes
?>

The HTML output of the above code is as follows (view source code):

<!DOCTYPE html>
<html>
<body>
Bill & 'Steve'<br>
Bill & 'Steve'<br>
Bill & 'Steve'
</body>
</html>

Browser output of the above code:

Bill & 'Steve'
Bill & 'Steve'
Bill & 'Steve'

Example 2

Convert predefined HTML entities to double quotes:

<?php
$str = 'I love "PHP".';
echo htmlspecialchars_decode($str, ENT_QUOTES); // Convert double and single quotes
?>

The HTML output of the above code is as follows (view source code):

<!DOCTYPE html>
<html>
<body>
I love "PHP".
</body>
</html>

Browser output of the above code:

I love "PHP".