Search code examples
phpsubstr

How to use htmlentities with substring


Can anyone explain why this substr is not working ? What am I missing ?

<?= substr(htmlentities($movie->movie_name) , 0 , 10) ?>

Solution

  • You probably want to switch the order of these function calls, like this:

    <?= htmlentities(substr($movie->movie_name, 0, 10)); ?>
    

    htmlentities replaces special characters, such as < with &lt;. Where < was only one character for the substr function, &lt; took 4 spaces. Even worse, it could be cut of halfway, where you end up with The big &l.