How to convert special HTML entities back to characters in PHP

How to convert special HTML entities back to characters in PHP

You can utilize the PHP htmlspecialchars_decode() capacity to change over the uncommon HTML substances, for example &amp;&lt;&gt;and so on back to the typical characters for example (i.e. &<>).. The htmlspecialchars_decode() work is inverse of the htmlspecialchars() work which changes over unique HTML characters into HTML substances. 


(PHP 4, PHP 5, PHP 7)

htmlspecialchars - Convert special characters to HTML entities


htmlspecialchars (string $ string [, int $ flags = ENT_COMPAT | ENT_HTML401 [, string $ encoding = ini_get ("default_charset") [, bool $ double_encode = TRUE]]]): string

Some characters have special meanings in HTML, and must be replaced by HTML entities to keep their meanings. This function returns a character string with these modifications. If you need all input substrings that are associated with named entities to be transformed, use the htmlentities () function.

We should look this exemple:

  • <?php
  • $my_str = "I&#039;ll come &amp; &lt;b&gt;&quot;get you&quot;&lt;/b&gt;.";
  • // Decode &amp;, &lt;, &gt; and &quot;
  • echo htmlspecialchars_decode($my_str);
  • // Decode &amp;, &lt;, &gt;, &quot; and &#039;
  • echo htmlspecialchars_decode($my_str, ENT_QUOTES);
  • // Decode &amp;, &lt; and &gt;
  • echo htmlspecialchars_decode($my_str, ENT_NOQUOTES);
  • ?>

if you want to improve the solution
comment to help the community
No comments
Post a Comment

Post a Comment