For filtering special characters, I have 80 points.

Source: Internet
Author: User
I have 80 points for filtering Special characters .. I have encountered a problem. when submitting a form, filter out the submitted content with special characters such :~! @ # $ % ^ & Amp; * () _ + & nbsp ;.. for example, I have submitted the following content: Life * & amp;) can) (_ how to filter out more beautiful: Life can be more beautiful. My practice is: $ str = str_replace (for filtering Special characters... I have 80 points ..
I have encountered a problem. when submitting a form, filter out the submitted content with special characters such :~! @ # $ % ^ & * () _ + ..
If I submit a message: Life * &) can) (_ more beautiful

How to filter out: Life can be more beautiful.

My approach is:
$ Str = str_replace ("&", "", $ str );
$ Str = str_replace (">", "", $ str );
$ Str = str_replace ("<", "", $ str );
$ Str = str_replace ("=", "", $ str );
$ Str = str_replace ("(", "", $ str );
$ Str = str_replace (")", "", $ str );
$ Str = str_replace ("[", "", $ str );
$ Str = str_replace ("]", "", $ str );
$ Str = str_replace (".", "", $ str );
$ Str = str_replace ("*", "", $ str );
$ Str = str_replace ("#", "", $ str );
$ Str = str_replace ("$", "", $ str );
$ Str = str_replace ("@", "", $ str );
$ Str = str_replace ("-", "", $ str );
$ Str = str_replace ("+", "", $ str );
$ Str = str_replace ("&", "", $ str );
$ Str = str_replace ("! "," ", $ Str );
$ Str = str_replace ("~ "," ", $ Str );
$ Str = str_replace ("^", "", $ str );
$ Str = str_replace ("%", "", $ str );
$ Str = str_replace ("'", "", $ str );
$ Str = str_replace ("\" "," ", $ str );

Is there a simpler method .. Please advise .. I have 80 points .. Share:
------ Solution --------------------
Reference:
Why: $ str = preg_replace ("['~! @ # $ % ^ & * () + =
------ Solution --------------------
{} ':;', // [//]. <> /?~! @ # ¥ % ...... & * () -- +
------ Solution --------------------
{} [] ';: "'., ,?] ",'', $ Str); can't you write it like this?


1. the biggest problem is that the regular expression does not have a boundary.
2. escape is not // But \, and the double quotation marks are \\
3.
------ Solution --------------------
Is regarded as or selected
4. Are there double quotation marks in double quotation marks?
......
------ Solution --------------------
$ P = str_split ("& ><= () []. * # $ @-+ &! ~ ^ % '\"_");
$ S = 'life * &) can) (_ more beautiful ';
Echo str_replace ($ p, '', $ s );
Life can be more beautiful

------ Solution --------------------
Reference:
Thank you for your advice. I just don't know whether the character in the full-width corner can be used .. I will try


Use the following function to change the width.

 function mb_str_split( $string, $encoding='UTF-8' ) { 
    # Split at all position not after the start: ^ 

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.