2010-05-31 62 views

回答

10

您可以使用正則表達式:

if (preg_match("^[0-9A-Za-z_]+$", username) == 0) { 
    echo "<p>Invalid username</p>"; 
} 
5

嘗試:

<?php 
function IsSafe($string) 
{ 
    if(preg_match('/[^a-zA-Z0-9_]/', $string) == 0) 
    { 
     return true; 
    } 
    else 
    { 
     return false; 
    } 
} 
?> 
+1

你還不如返回(的preg_match( '/ [^ A-ZA-Z0-9 _] /',$字符串)== 0) – Artefacto 2010-05-31 01:34:46

+0

@Art技術上'preg_match'返回一個int,所以有一個小的差異 – 2010-05-31 05:27:25

+0

@Michael Mrozek你需要再次閱讀我的評論。 – Artefacto 2010-05-31 11:55:47