Forcing a whole string to match a regular expression

Any questions involving matching text strings to patterns - the pattern is called a "regular expression."

Moderator: General Moderators

Post Reply
Citizen
Forum Contributor
Posts: 300
Joined: Wed Jul 20, 2005 10:23 am

Forcing a whole string to match a regular expression

Post 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!
User avatar
vigge89
Forum Regular
Posts: 875
Joined: Wed Jul 30, 2003 3:29 am
Location: Sweden

Post 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
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

Code: Select all

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

Edit: Vigge beat me to it. :P
Citizen
Forum Contributor
Posts: 300
Joined: Wed Jul 20, 2005 10:23 am

Post by Citizen »

But what about wierd characters like these?

« and ¤ and »
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post 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.
User avatar
GeertDD
Forum Contributor
Posts: 274
Joined: Sun Oct 22, 2006 1:47 am
Location: Belgium

Post 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)
Citizen
Forum Contributor
Posts: 300
Joined: Wed Jul 20, 2005 10:23 am

Post 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/
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

Citizen wrote:Still returns an error/
It shouldn't. What encoding does your page use?
Citizen
Forum Contributor
Posts: 300
Joined: Wed Jul 20, 2005 10:23 am

Post 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.
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

Your DOCTYPE doesn't make a difference.

What is the character encoding?
Citizen
Forum Contributor
Posts: 300
Joined: Wed Jul 20, 2005 10:23 am

Post by Citizen »

I think its UTF-8.... where would that be specified?
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post 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" />
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Post 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".
There are 10 types of people in this world, those who understand binary and those who don't
User avatar
GeertDD
Forum Contributor
Posts: 274
Joined: Sun Oct 22, 2006 1:47 am
Location: Belgium

Post 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
Post Reply