Laravel 5: Encoding HTML With the

Laravel 5: Encoding HTML With the "e" Helper Function

Laravel 5

The e function is a simple wrapper of PHP's htmlentities function. The e function utilizes the UTF-8 character encoding. The e function will sanitize user input when displaying it to the browser.

Signature

The signature of the e function is:

function e(
    $value
);

Example Use

Let's assume that a malicious user was posting on a forum and set the subject of their post to this:

<script>alert("hello everyone");</script>

If the forum software did not sanitize user output, perfectly valid JavaScript code would be sent to the browser. Since browsers are overly happy to execute any JavaScript included in the document, any forum visitor would see an alert box with hello everyone every time a page was loaded that displayed that particular forum post.

To prevent this, use the e function to sanitize user input when sending it to the browser:

$unsafeClientCode =
    '<script>alert("hello everyone");</script>';

$safeClientCode   = e($unsafeClientCode);

The value of $safeClientCode would then be:

&lt;script&gt;alert(&quot;hello everyone&quot;);&lt;/script&gt;gt;

At this point the browser will render a string that literally represents what they had typed.

Working With Htmlable Values

The e helper function will encode the HTML characters within a string value, but treats instances of "Illuminate\Contracts\Support\Htmlable" differently in that it will not encode the HTML characters in the Htmlable instance's toHtml() return value.

use Illuminate\Contracts\Support\Htmlable;

class CustomHtmlable implements Htmlable
{

    public function toHtml()
    {
        return '<p>This is a value</p>';
    }

}

// Create a new instance.
$instance = new CustomHtmlable;

// The value will still contain the HTML tags.
$returnValue = e($instance);

When building Htmlable value types, we can invoke the e function within the class itself to ensure the results of the toHtml method are client safe to return to the browser. In the following example, we are going to create a simple Person type to hold information about various people we might encounter; additionally, we will create a utility class PersonRow that will be used to represent a Person instance as a HTML table row.

use Illuminate\Contracts\Support\Htmlable;

class Person
{
    public $firstName;
    public $lastName;
}

class PersonRow implements Htmlable
{

    private $person;

    public function withPerson(Person $person)
    {
        $this->person = $person;

        return $this;
    }

    public function toHtml()
    {
        return '<tr>
                    <td>'.e($this->person->firstName) .'</td>
                    <td>'.e($this->person->lastName)  .'</td>
                </tr>';
    }

}


In the code above, we are invoking the e function on the values we are inserting into our HTML template. If we executed code similar to this:

$person = new Person;
$person->firstName = '<script>alert("Hello");</script>';
$person->lastName  = 'Doe';

$clientCode = with(new PersonRow)->withPerson($person)->toHtml();

Once the code has executed, the $clientCode variable would contain a value similar to the following output:


<tr> <td>&lt;script&gt;alert(&quot;Hello&quot;);&lt;/script&gt;</td> <td>Doe</td> </tr>

Start the Discussion

Leave a comment

Subscribe to our newsletter