Converting a HTML String to Entities

Problem

You want to "escape" html in your web page output.

You know you can use the PHP htmlentities() method, but want to do it the Laravel way.

Solution

Use the HTML::entities() method.

echo HTML::entities('<h1>Title example</h1>');

The above will convert the less than symbols to &lt; and the greater than symbols to &gt;

You can also use the helper function e().

echo e('<h1>Title example</h1>');

The above will produce the same output as the longer HTML::entities() method.

Discussion

This method actually calls htmlentities().

Specifically it calls htmlentities($your_string, ENT_QUOTES, 'UTF-8', false).

This will convert quotes (both single and double), use UTF-8 as the character encoding and won't convert entities in the string already converted.

comments powered by Disqus