PHP/HTML/htmlspecialchars

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

Escaping HTML entities

 
<?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);
?>



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

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

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



htmlspecialchars.php

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



UTF-8 HTML encoding

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