Search code examples
phphtmlzend-framework2html-escape-characters

PHP won't escape HTML special characters


In a ZF2 application, I have a .phtml layout containing the following:

<p>Created in 2015 <?php echo htmlspecialchars("by Jérôme Verstrynge",ENT_HTML5); ?></p>

Yet, when I check the source of returned pages, I get:

<p>Created in 2015 by Jérôme Verstrynge</p>

instead of

<p>Created in 2015 by J&eacute;r&ocirc;me Verstrynge</p>

Why?


Solution

  • As per the manual, htmlspecialchars() only translates &, ", ', < and >. I think you are confusing it with htmlentities().

    ZF2 also has an escaper view helper with a lot more options: http://framework.zend.com/manual/current/en/modules/zend.escaper.escaping-html.html