Help with preg_match/regex
I don't know regex, so please help. For the following code:
$req_user = trim($_GET['user']);
if(!preg_match("^([0开发者_JAVA百科-9a-z])+$", $req_user)){
//do something ...
}
I get this error: NOTICE: No ending delimiter '^' found.
When giving a regex in php you need to put a matching character at the beginning and end of the string to delimit it. So, the complaint is that it sees a ^ at the start of the string, and assumes it is the delimiter, but there is no matching character at the end. As such, you really need a string like
"#^([0-9a-z])+$#"
when entering the regex in php.
$req_user = trim($_GET['user']);
if(!preg_match("!^([0-9a-z])+$!", $req_user)){
//do something ...
}
You have to add an Char on the start and end of the regexp to have it working. This char restricts the regexp-area. After the last ! you can add modifiers for case-ignore etc.
精彩评论