Page is a not externally linkable
- Code, Content, and Presentation
-- PHP Server Side Scripting
---- preg match


rocknbil - 7:58 pm on Dec 14, 2009 (gmt 0)


i have a preg_match to check to see if value of $id is a digit like this:

First, is zero a valid digit in your scenario?

If it's "anything but zero" you don't need a preg. is_numeric() works, but that includes zero.

if ($var > 0) {
// simple enough
}

You can use is_numeric if zero is included, or

Do this first to make sure user hasn't errantly included a space or touched some other key:
$var = preg_replace('/[^\d]+/','',$var);

then

if (preg_match('/^\d+$/',$var)) { begins and ends with any number of digits

if (preg_match('/^\d$/',$var)) { begins and ends with exactly one digit

if (preg_match('/^\d{2}$/',$var)) { begins and ends with exactly two digits

if (preg_match('/^\d{4,7}$/',$var)) { begins and ends with anywhere from four to seven digits

i am using the following code

Try this.

// top of script for ease of use:
$bannedNames = Array (
'secret',
'x',
'spam-boy'
);
// This is anything NOT these characters; that is,
// it will throw away anything NOT in this list.
// No need for a-z, we will use case insensitive modifier.
$allowed_chars = 'A-Z0-9\-_'; // Escape dash, not a range

$isValid=1;
$name=preg_replace("/[^$allowed_chars]+/i",$_POST['new_user_name']); // Do this first!
foreach ($bannedNames as $nm) {
if (preg_match("/^$nm$/i",$name)) { $isValid=0; } // note case insensitive
if ($isValid==0) { break; }
}
if ($isValid==0) {
echo "Please use the message board properly and enter a valid name. Inappropriate use will result in a ban. ";
exit;
}


Thread source:: http://www.webmasterworld.com/php/4042650.htm
Brought to you by WebmasterWorld: http://www.webmasterworld.com