Page 1 of 1

Forcing a whole string to match a regular expression

Posted: Sat Aug 04, 2007 12:06 pm
by Citizen
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!

Posted: Sat Aug 04, 2007 1:05 pm
by vigge89
http://php.net/manual/en/reference.pcre ... syntax.php
^ = assert start of subject (or line, in multiline mode)
$ = assert end of subject (or line, in multiline mode)
Example pattern:

Code: Select all

~^[a-z0-9_]+$~i

Posted: Sat Aug 04, 2007 1:07 pm
by superdezign

Code: Select all

^[A-Za-z0-9]$
Whole string.

Edit: Vigge beat me to it. :P

Posted: Sat Aug 04, 2007 2:57 pm
by Citizen
But what about wierd characters like these?

« and ¤ and »

Posted: Sun Aug 05, 2007 8:24 am
by superdezign
Citizen wrote:But what about wierd characters like these?

« and ¤ and »
Have you tried adding them into the regex?
You could also process the data with htmspecialchars, and just allow the ampersand and semicolon.

Posted: Mon Aug 13, 2007 10:41 am
by GeertDD
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)

Posted: Tue Aug 14, 2007 1:52 pm
by Citizen
No luck....

Code: Select all

if ( !preg_match('/^[A-Za-z0-9_«»¤]+$/D', $newname) ) { 
		$message = "Invalid new username: only alpha numeric characters allowed."; 
		$error += 1;
	}
Still returns an error/

Posted: Tue Aug 14, 2007 3:04 pm
by superdezign
Citizen wrote:Still returns an error/
It shouldn't. What encoding does your page use?

Posted: Tue Aug 14, 2007 3:41 pm
by Citizen

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">
Just to clarify:

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.

Posted: Tue Aug 14, 2007 3:50 pm
by superdezign
Your DOCTYPE doesn't make a difference.

What is the character encoding?

Posted: Wed Aug 15, 2007 12:29 pm
by Citizen
I think its UTF-8.... where would that be specified?

Posted: Wed Aug 15, 2007 1:00 pm
by superdezign
Citizen wrote:I think its UTF-8.... where would that be specified?
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.

Code: Select all

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

Posted: Wed Aug 15, 2007 4:42 pm
by VladSun
superdezign wrote:
Citizen wrote:I think its UTF-8.... where would that be specified?
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.

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".

Posted: Thu Aug 16, 2007 5:50 am
by GeertDD

Code: Select all

header('Content-type: text/html; charset=UTF-8');
In Firefox have a look at the Page Info window to see what charset is set in the header.

http://www.phpwact.org/php/i18n/charsets