what is differences between htmlspecialchars() and htmlentities()

Convert the predefined characters "<" (less than) and ">" (greater than) to HTML entities:

File name : index.php

<?php
$str = "This is some <b>bold</b> text.";
echo htmlspecialchars($str);
?>

Output :-

The HTML output of the code above will be (View Source):

<!DOCTYPE html>
<html>
<body>
This is some &lt;b&gt;bold&lt;/b&gt; text.
</body>
</html>

The browser output of the code above will be:
This is some bold text.

The htmlspecialchars() function converts some predefined characters to HTML entities.

The predefined characters are:

  • & (ampersand) becomes &
  • " (double quote) becomes "
  • ' (single quote) becomes '
  • < (less than) becomes <
  • > (greater than) becomes >
  • Tip: To convert special HTML entities back to characters, use the htmlspecialchars_decode() function.


    htmlentities()

    File name : index.php

    <?php
    $input_string="PHP 'character string conversion' functions <i>htmlentities()</i>";
    $output = htmlentities($input_string);
    echo "<b>Original Character String</b><br/>";
    echo $input_string."<br/><br/>";
    echo "<b>After Conversion</b><br/>";
    echo $output;
    ?>

    In the above program, we have input string which includes single quotes and less than and greater than symbols which can be parse by the browser. So, before applying these input into htmlentities() function, if we print it to the browser, then, the browser will parse the html tags, <i></i>, and there by displays the string htmlentities() with italic font.





    Previous Next


    Trending Tutorials




    Review & Rating

    0.0 / 5

    0 Review

    5
    (0)

    4
    (0)

    3
    (0)

    2
    (0)

    1
    (0)

    Write Review Here