Web Hosting Talk







View Full Version : PCRE Expression


akasixcon
04-07-2007, 01:58 AM
vBulletin PCRE Questions
I want to disable certain things when a user registers on my forum which requires PCRE expression.

How do I make it so that a users registrations are limited,
basically disable spaces and only allow letters and numbers in the username.

Yeah thanks for the help anyway.


I need also a repetivive to mininum/ output settings. For example if I only put [0-9] on the PCRE box it disables ONLY THE FIRST character of the input.

Meaning if I put 3down, it wont allow it.

however if I put d3own, do3wn, down3 etc.... it will be allowed.


But yeah if someone can give me a code that I can test out then that would be great.

feedsfarm
04-07-2007, 05:45 AM
function isValidHandle($str) {
return preg_match('#^[0-9A-Za-z]+$#', $str);
}

If you also wanted a limit on handle length:

function isValidHandle($str) {
return preg_match('#^[0-9A-Za-z]{6,12}$#', $str);
}

Where 6=min, and 12=max lengths.

Here's a Unicode variation that is useful for multilingual websites (requires a recent version of PCRE):

function isValidHandleUnicode($str) {
return preg_match('#^[\p{L}\p{N}]{6,12}$#u', $str);
}

akasixcon
04-07-2007, 11:38 AM
function isValidHandle($str) {
return preg_match('#^[0-9A-Za-z]+$#', $str);
}

If you also wanted a limit on handle length:

function isValidHandle($str) {
return preg_match('#^[0-9A-Za-z]{6,12}$#', $str);
}

Where 6=min, and 12=max lengths.

Here's a Unicode variation that is useful for multilingual websites (requires a recent version of PCRE):

function isValidHandleUnicode($str) {
return preg_match('#^[\p{L}\p{N}]{6,12}$#u', $str);
}
Thanks.

I found a simpler way: Simple Code
^[a-z0-9-_]+$

Now a new question: How do I make that match codes from multiple lines? Lets say that is just a question on inputting your hobbies.

How do I make it so that it will match:

Surfing
Snowboarding
Skateboarding


but make it not match:

h4cking
war3z
stea1ing

Basically in 2nd example I want my original SIMPLE CODE tp be repeated over a multiple line textbox.

feedsfarm
04-07-2007, 12:39 PM
preg_match('#^[0-9a-z]+$#m', $str);

Note the m flag in the function.

akasixcon
04-07-2007, 02:10 PM
I don't quite have much experience with PCRE / regexp in fact I just started learning it.

Which part of that code do I copy thats by itself that can match? again I'm using vbulletin so for example the code:

[a-z]* or [0-9]+ would work , I don't need to put preg_match and stuff.

Ks Jeppe
04-07-2007, 03:48 PM
akasixcon, the vbulletin boxes, are they for what's *not* allowed, or for what *is* allowed?

If it's for whats is allowed, then i think you'd put ^[a-z0-9\n]*$ i think... (\n is the way you define a new line), try that out? or perhaps ^[a-z0-9]*$m as feedsfam said..