Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

htmlentities()

htmlentities() is a native PHP function which converts special characters in a string to their equivalent in HTML.

htmlentities() takes the string to be encoded, as an argument. It also accepts an argument $encoding to specify what the original encoding of the string is, in particular when it is not utf-8, which is the default configuration.

There are options to control which characters shall be encoded: for example, single and double quotes may be converted or not, with ENT_QUOTES and ENT_NOQUOTES, respectively.

Finally, $double_encode makes htmlentities() skip encoding a second time any entities already set in the string.

The opposite of htmlentities() is html_entity_decode().

<?php

    $str = "A 'quote' is <b>bold</b>";
    
    echo htmlentities($str);
    echo "\n\n";
    echo htmlentities($str, ENT_COMPAT);

?>

Documentation

See Also