Python-like PHP htmlspeialchars () filter String functions

Source: Internet
Author: User

1: Escape () function using the CGI module

>>>Import CGI>>> Stra ='<foo>\nfoo\t "Bar"'>>> Resa =Cgi.escape (stra)>>>print (RESA)&lt;foo&Gt;foo"Bar">>> STRB ="<foo>\nfoo\t ' Bar '">>> Resb =Cgi.escape (STRB)>>>print (RESB)&lt;foo&Gt;foo'Bar'

The single and double quotation marks in the visible string are not escaped as character entities, viewing the document

Escape (S, quote=None)    "&" "<""  >" to html-safe sequences.      is true, the quotation mark character (")is     also translated.

If a quote=true is given, then the double quotes are escaped, but the single quotation marks do not;

RESAA = Cgi.escape (stra,true)>>> Print (RESAA)&lt;foo&gt;foo    & quot;bar&quot;    #这里转义了双引号>>> resbb = cgi.escape (strb,true)>>> Print (RESBB)&lt;foo &gt;foo    'bar' # There's no escaping here

2: Escape method using HTML module

>>> Import HTML'<foo>\nfoo\t ' bar' >>> resa =  Html.escape (stra)>>> Print (resa)&lt;foo&gt;foo    &quot;bar&  quot;  #这里做了转义处理   "<foo>\nfoo\t ' bar">>> resb =  Html.escape (STRB)>>> Print (resb)&lt;foo&gt;foo    

The escape method of the HTML module defaults to handling single and double quotes, the document:

 Escape (S, Quote=true) Replace special characters   " &  " ,  <  "  and  " >  "  to Html-safe sequences. If the optional flag quote  is  true  ( The default  ), the quotation mark characters, both  double  quote ( " ) and single quote (') characters is also  translated. 

Python-like PHP htmlspeialchars () filter String functions

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.