PHP/HTML/htmlspecialchars

Материал из Web эксперт
Перейти к: навигация, поиск

Escaping HTML entities

   <source lang="html4strict">

<?php $html = "<a href="fletch.html">Stew"s favorite movie.</a>\n"; print htmlspecialchars($html); print htmlspecialchars($html, ENT_QUOTES); print htmlspecialchars($html, ENT_NOQUOTES); ?>

 </source>
   
  


htmlspecialchars() function converts a select few characters in the context of HTML into their equivalent HTML entities.

   <source lang="html4strict">

Its syntax is: string htmlspecialchars (string string) The htmlspecialchars() function currently only converts the following characters: & becomes &amp "" becomes &quot < becomes < > becomes &gt

<? $user_input = "<<enough>> & !"; $conv_input = htmlspecialchars($user_input); print $conv_input; ?>

 </source>
   
  


htmlspecialchars.php

   <source lang="html4strict">

<?php

  $input = "<<enough>>";
  echo htmlspecialchars($input);

?>

 </source>
   
  


UTF-8 HTML encoding

   <source lang="html4strict">

<?php $encoded_name = htmlspecialchars($_POST["name"], ENT_QUOTES, "UTF-8"); $encoded_dinner = htmlentities($_POST["dinner"], ENT_QUOTES, "UTF-8"); ?>

 </source>