Web development methods for filtering JavaScript and HTML

Source: Internet
Author: User

JavaScript filtering methods:

The first scenario: Use the Htmlspecialchars function to convert special characters and use the NL2BR function to insert some necessary <br/> tags.

1 $comment = <<<eof  2 <script type= "Text/javascript" >  3  while (true) {  4     alert (' I play! ');   5 }  6 </script>  7 EOF;  // If $comment is a comment   8 // filter JavaScript code   9 Echo $comment;

The output is:

&lt;script type=&quot;text/javascript&quot;&gt;<br/> while (true) {<br/ >    alert (' I play! '); <br/>}<br/>&lt;/script&gt;

The second option: Remove all the <script...>,</script> from the comment content.

1 // get rid  of all the <script...>,</script> in the comments

The output is:

 while (true) {      alert (' I play! ');  }  

HTML Filtering methods:

The first option: to escape the symbol <>, or delete it directly.

1 $comment = preg_replace ("/<[\/\!") *? [^<>]*?>/si ",", $comment);  

The second scenario: Use the Strip_tags () function.

1 <? PHP 2 Echo strip_tags ("Hello <b>world!</b>"); 3 ?>

Web development methods for filtering JavaScript and HTML

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.