Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP htmlspecialchars is not working [closed]

<?php  
    $new = htmlspecialchars("<a href='test'>Test</a>", ENT_QUOTES);  
    echo $new;  
?>

output should be

& lt;a href=& #039;test& #039;&gt;Test& lt;/a& gt; 

but output is

&lt;a href=&#039;test&#039;&gt;Test&lt;/a&gt;
like image 222
Wasim A. Avatar asked Feb 25 '11 07:02

Wasim A.


People also ask

What's the difference between Htmlentities () and htmlspecialchars ()?

Difference between htmlentities() and htmlspecialchars() function: The only difference between these function is that htmlspecialchars() function convert the special characters to HTML entities whereas htmlentities() function convert all applicable characters to HTML entities.

What is the purpose of the Htmlspecialchars () function?

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

Does Htmlspecialchars prevent XSS?

Using htmlspecialchars() function – The htmlspecialchars() function converts special characters to HTML entities. For a majority of web-apps, we can use this method and this is one of the most popular methods to prevent XSS. This process is also known as HTML Escaping.

Do I need Htmlspecialchars?

The htmlspecialchars() function is incredibly useful in PHP, especially when you have text you intend to output. You can easily convert any special characters to their HTML entity equivalent using this function. One of the key reasons you will want to do this is to try and prevent XSS.


1 Answers

Don't worry. htmlspecialchars() is encoding the < and > characters properly. It is just that when you echo the encoded string to your computer screen, your browser helpfully decodes the characters again. If you view the page source you will see the encoded string.

like image 129
Peter Carter Avatar answered Oct 05 '22 23:10

Peter Carter