📜  htmlspecialchars_decode (1)

📅  最后修改于: 2023-12-03 14:41:59.130000             🧑  作者: Mango

htmlspecialchars_decode

The htmlspecialchars_decode function is a built-in PHP function used to decode HTML entities back to their corresponding characters. It is the opposite of the htmlspecialchars function, which is used to convert special characters to HTML entities.

Usage
string htmlspecialchars_decode ( string $string [, int $flags = ENT_COMPAT | ENT_HTML401 [, string $encoding = ini_get("default_charset") ]] )

The htmlspecialchars_decode function takes a string as input and returns the original string with HTML entities decoded. It performs the following conversions:

  • '&' (ampersand) becomes '&'
  • '"' (double quote) becomes '"'
  • ''' (single quote / apostrophe) becomes ''' (when ENT_QUOTES flag is set)
  • '<' (less than) becomes '<'
  • '>' (greater than) becomes '>'
Parameters
  • string (required): The string to decode.
  • flags (optional): This parameter specifies the conversion behavior. It can be a combination (using the bitwise OR operator) of the following flags:
    • ENT_COMPAT: Default. Converts double-quotes and leave single-quotes unconverted.
    • ENT_QUOTES: Converts both double and single quotes.
    • ENT_NOQUOTES: Leaves both double and single quotes unconverted.
    • ENT_HTML401: Default. Replace entities with their HTML 4.01 character set equivalents.
    • ENT_XML1: Replace entities with their XML 1.0 character set equivalents.
Return Value

The htmlspecialchars_decode function returns the decoded string. If the input string is NULL, an empty string, or not a string type, it will return NULL.

Example
$html = "&lt;p&gt;Hello &amp; World!&lt;/p&gt;";
$decodedString = htmlspecialchars_decode($html);

echo $decodedString;

Output:

<p>Hello & World!</p>

In this example, the htmlspecialchars_decode function is used to decode the HTML entities in the $html string and store the decoded string in the $decodedString variable. The output is then echoed, showing the original HTML string with the entities decoded.

Conclusion

The htmlspecialchars_decode function is handy for reversing the conversion of HTML entities back to their original characters. It is useful when dealing with data that requires proper HTML rendering or when working with data received from external sources, such as user inputs or databases.