Forcing a whole string to match a regular expression
Moderator: General Moderators
Forcing a whole string to match a regular expression
I've used regular expressions before, but never to check to see if a WHOLE string matches a regular expression.
For instance, I want to make sure that a WHOLE user's name matches A-Za-z0-9 and underscores.
Also, what if I wanted to include « and ¤ and » ?
Any help would be greatly appreciated!
For instance, I want to make sure that a WHOLE user's name matches A-Za-z0-9 and underscores.
Also, what if I wanted to include « and ¤ and » ?
Any help would be greatly appreciated!
http://php.net/manual/en/reference.pcre ... syntax.php
Example pattern:^ = assert start of subject (or line, in multiline mode)
$ = assert end of subject (or line, in multiline mode)
Code: Select all
~^[a-z0-9_]+$~i- superdezign
- DevNet Master
- Posts: 4135
- Joined: Sat Jan 20, 2007 11:06 pm
- superdezign
- DevNet Master
- Posts: 4135
- Joined: Sat Jan 20, 2007 11:06 pm
Add all characters you want to allow to the character class.
Code: Select all
var_dump(preg_match('/^[A-Za-z0-9_«»¤]+$/D', '«_¤_»')); // int(1)No luck....
Still returns an error/
Code: Select all
if ( !preg_match('/^[A-Za-z0-9_«»¤]+$/D', $newname) ) {
$message = "Invalid new username: only alpha numeric characters allowed.";
$error += 1;
}- superdezign
- DevNet Master
- Posts: 4135
- Joined: Sat Jan 20, 2007 11:06 pm
Code: Select all
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en">Code: Select all
if ( !preg_match('/^[A-Za-z0-9_«»¤]+$/D', $newname) ) {
$message = "Invalid new username: only alpha numeric characters allowed.";
$error += 1;
}Returns:
Invalid new username: only alpha numeric characters allowed.
- superdezign
- DevNet Master
- Posts: 4135
- Joined: Sat Jan 20, 2007 11:06 pm
- superdezign
- DevNet Master
- Posts: 4135
- Joined: Sat Jan 20, 2007 11:06 pm
Hehe. If it isn't specified, the browser decides what it will be. You need to specify it. I think It can be done through headers, but do it through a meta tag.Citizen wrote:I think its UTF-8.... where would that be specified?
Code: Select all
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />It would work unless you use Apche and have its directive "AddDefaultCharset" set to some other charset... In this case the easiest solution is to set this directive to "Off".superdezign wrote:Hehe. If it isn't specified, the browser decides what it will be. You need to specify it. I think It can be done through headers, but do it through a meta tag.Citizen wrote:I think its UTF-8.... where would that be specified?
Code: Select all
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
There are 10 types of people in this world, those who understand binary and those who don't
Code: Select all
header('Content-type: text/html; charset=UTF-8');http://www.phpwact.org/php/i18n/charsets