關於特殊字元的過濾...我這有80分。。

來源:互聯網
上載者:User
我現遇到一個問題,在提交表單時,對提交的內容要過濾掉特殊字元如:~!@#$%^&*()_+ ..這些
如我有一提交內容:生活*&)可以)(_更美的

如何過濾成:生活可以更美的。

我的做法是:
$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);

有沒有更簡便的方法。。求指教。。我這有80分。。


回複討論(解決方案)

為何:$str=preg_replace("[`~!@#$%^&*()+=|{}':;',//[//].<>/?~!@#¥%……&*()??+|{}【】‘;:”“’。,、?]",'',$str);這樣寫,不行呢?

為何:$str=preg_replace("[`~!@#$%^&*()+=|{}':;',//[//].<>/?~!@#¥%……&*()??+|{}【】‘;:”“’。,、?]",'',$str);這樣寫,不行呢?

1.最大問題是正則沒有邊界符
2.轉義不是//而是\,雙引號內是\\
3.|被看作是或選擇了
4.雙引號內還有雙引號麼
……

大哥,那要怎麼弄才行呀。

$p = str_split("&><=()[].*#$@-+&! ~ ^%'\"_");$s = '生活*&)可以)(_更美的';echo str_replace($p, '', $s);
生活可以更美的

明白了,多謝指點...只是不知全形的特符字元如¥)(,這些是否也能適用。。我試下

明白了,多謝指點...只是不知全形的特符字元如¥)(,這些是否也能適用。。我試下

全形的話改成用下面的函數

 # Prints: Array (     [0] => 火     [1] => 車     [2] => 票 )

對於切割串為數組,全形不適合
但手工書寫的數組就是一樣的了

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    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.