| | |
preg_match validation help please
Please support our PHP advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
![]() |
•
•
Join Date: Feb 2005
Posts: 4
Reputation:
Solved Threads: 0
Hello, can someone please help me with this string? Right now, it will validate keywords separated by commas, lowercase only, and any two digit numbers by themselves also separated by commas. How do I further modify it to permit /up to/ two keywords, trimming any extra spaces that might be entered?
<?php if (preg_match('/^(([a-z]+|\d{1,2})\s*,\s*)*([a-z]+|\d{1,2})$/', $string)) { [do something] } else { [do something else] ?>
<?php if (preg_match('/^(([a-z]+|\d{1,2})\s*,\s*)*([a-z]+|\d{1,2})$/', $string)) { [do something] } else { [do something else] ?>
what might be easier than usign that huge ugly regular expression is if you first break up your keywords
I have not tested any of this so it still might need a slight amount of tweaking but this should give you the idea you need to get going from here. If you want to get rid of ALL spaces not just trim a keyword then replace the
$keywords[$i] = trim($keywords[$i]); with this instead
$keywords[$i] = ereg_replace(" ",$keywords[$i]);
I have not tested any of this so it still might need a slight amount of tweaking but this should give you the idea you need to get going from here. If you want to get rid of ALL spaces not just trim a keyword then replace the
$keywords[$i] = trim($keywords[$i]); with this instead
$keywords[$i] = ereg_replace(" ",$keywords[$i]);
PHP Syntax (Toggle Plain Text)
//this will seperate every keyword into an array $keywords = explode("," , $string); //next let's trim any whitespace for ($i=0; $i<sizeOf($keywords); $i++) $keywords[$i] = trim($keywords[$i]); $i = 0; $validKeyWord = true; while (sizeOf($keywords) < $i && $validKeyWord) { if (ereg("^([a-z]+)|[0-9]{2,2})$", $keyword[$i++])) $validKeyWord= false } if ($validKeyWord) { if (sizeOf($keywords) > 2) { echo "error! you are limited to only 2 keywords"; do_something_with_this_function($keywords); } else { echo "you have 2 keywords or less, good job"; do_whatever_your_site_does_with_keywords($keywords); } } else { echo "you did not enter a proper keyword, please try again"; }
•
•
Join Date: Feb 2005
Posts: 4
Reputation:
Solved Threads: 0
Thanks for working on this!
I seem to be getting an error, I am not sure how to fix (it looks like there may be an extra ")" or something on line 18: if (ereg("^([a-z]+)|[0-9]{2,2})$", $keyword[$i++])) <-- ?, but still gives a parse error upon removal:
I seem to be getting an error, I am not sure how to fix (it looks like there may be an extra ")" or something on line 18: if (ereg("^([a-z]+)|[0-9]{2,2})$", $keyword[$i++])) <-- ?, but still gives a parse error upon removal:
PHP Syntax (Toggle Plain Text)
<?php $keywords = "test,green,green,fun test ,trying,work work"; //this will seperate every keyword into an array $keywords = explode("," , $string); //next let's trim any whitespace for ($i=0; $i<sizeOf($keywords); $i++) $keywords[$i] = trim($keywords[$i]); $i = 0; $validKeyWord = true; while (sizeOf($keywords) < $i && $validKeyWord) { if (ereg("^([a-z]+)|[0-9]{2,2})$", $keyword[$i++])) $validKeyWord= false } if ($validKeyWord) { if (sizeOf($keywords) > 2) { echo "error! you are limited to only 2 keywords"; do_something_with_this_function($keywords); } else { echo "you have 2 keywords or less, good job"; do_whatever_your_site_does_with_keywords($keywords); } } else { echo "you did not enter a proper keyword, please try again"; } ?>
One thing I see, this:
[PHP] if (ereg("^([a-z]+)|[0-9]{2,2})$", $keyword[$i++]))
$validKeyWord= false[/PHP]
should probably be:
[PHP] if (ereg("^([a-z]+)|[0-9]{2,2})$", $keyword[$i++]))
{
$validKeyWord= false;
}[/PHP]
Dance
[PHP] if (ereg("^([a-z]+)|[0-9]{2,2})$", $keyword[$i++]))
$validKeyWord= false[/PHP]
should probably be:
[PHP] if (ereg("^([a-z]+)|[0-9]{2,2})$", $keyword[$i++]))
{
$validKeyWord= false;
}[/PHP]
Dance
•
•
Join Date: Feb 2005
Posts: 4
Reputation:
Solved Threads: 0
That fixed the error. I feel i'm a big step closer! This is much better than the original expression I brought going into this, at least with this I can see what's going on better. I know all those print strings I added in my code below look stupid, but it's helping me learn and test this out in the process. As I go along I can see exactly how it's outputting everything.
I have these questions:
1) It seems to be allowing uppercase and special characters.
2) It will allow more than two words in a keyphrase
........ so, "cool,word word,22" [is good],
........ not, "cool,word word word,22"
3) Finally, it should just allow (up to) two digit numbers to pass:
........ '2,33,44' is fine,
........ '2 2,33 33,444 444 444' should not clear this validation
Some other examples of what should/should not clear:
blue,green green,93 (good)
green green,blue blue,10 (good)
10,32,green (good)
10,32 32,green (no)
10,green green,32 (good)
blue green green,10,30 (no)
Of course, no uppercase or special characters anywhere.
One last thing, I would rather have this output the finished product back into a comma delimited string, not an array so I can pump it into a database like that.
Thanks so much
Julia.
I have these questions:
1) It seems to be allowing uppercase and special characters.
2) It will allow more than two words in a keyphrase
........ so, "cool,word word,22" [is good],
........ not, "cool,word word word,22"
3) Finally, it should just allow (up to) two digit numbers to pass:
........ '2,33,44' is fine,
........ '2 2,33 33,444 444 444' should not clear this validation
Some other examples of what should/should not clear:
blue,green green,93 (good)
green green,blue blue,10 (good)
10,32,green (good)
10,32 32,green (no)
10,green green,32 (good)
blue green green,10,30 (no)
Of course, no uppercase or special characters anywhere.
One last thing, I would rather have this output the finished product back into a comma delimited string, not an array so I can pump it into a database like that.
Thanks so much
Julia.
PHP Syntax (Toggle Plain Text)
<?php $string = "cool,cool too,22"; //this will seperate every keyword into an array $keywords = explode("," , $string); //next let's trim any whitespace for ($i=0; $i<sizeOf($keywords); $i++) $keywords[$i] = trim($keywords[$i]); $i = 0; $validKeyWord = true; while (sizeOf($keywords) < $i && $validKeyWord) { if (ereg("^([a-z]+)|[0-9]{2,2})$", $keyword[$i++])) $validKeyWord= false; } if ($validKeyWord) { if (sizeOf($keywords) > 3) { echo "error! you are limited to only 3 keywords"; ?><br><?php ?>:<?php echo $keywords[0]; ?>:<br><?php ?>:<?php echo $keywords[1]; ?>:<br><?php ?>:<?php echo $keywords[2]; ?>:<br><?php } else { echo "you have 3 keywords or less, good job"; ?><br><?php ?>:<?php echo $keywords[0]; ?>:<br><?php ?>:<?php echo $keywords[1]; ?>:<br><?php ?>:<?php echo $keywords[2]; ?>:<br><?php } } else { echo "you did not enter a proper keyword, please try again"; ?><br><?php ?>:<?php echo $keywords[0]; ?>:<br><?php ?>:<?php echo $keywords[1]; ?>:<br><?php ?>:<?php echo $keywords[2]; ?>:<br><?php } ?>
•
•
Join Date: Oct 2006
Posts: 82
Reputation:
Solved Threads: 0
•
•
•
•
Hello, can someone please help me with this string? Right now, it will validate keywords separated by commas, lowercase only, and any two digit numbers by themselves also separated by commas. How do I further modify it to permit /up to/ two keywords, trimming any extra spaces that might be entered?
<?php if (preg_match('/^(([a-z]+|\d{1,2})\s*,\s*)*([a-z]+|\d{1,2})$/', $string)) { [do something] } else { [do something else] ?>
I just started working with preg_match too.
[a-z] is lower case
[A-Z] is upper case
[a-zA-Z] upper and lower case
If I am not mistaken {1,2} is limiting you to two two characters seperated by comma.
Here is an article to look at.
Now what's that really do?
/[^a-zA-Z0-9\.\-\ß\ä\Ä\ü\Ü\ö\Ö\ ]+$/
The slashes "/" and "/" are delimiters, "^" marks the start of string or line and the Dollar sign "$" the end of the string, or line. The plus-symbol "+" means required.
Knowing what the special characters mean, it actually says the following: A string, from start to finish, may contain this characters (a to z (lower case), A to Z (upper case), the numbers from 0 to 9, a dot ("."), a hiven ("-") and the special characters ä, ö ü (both upper and lower case) and space (" ")), and these characters only.
preg_match() is a case sensitiv function, which means it treats "a" and "A" differently. I included upper ("A-Z") and lower case ("a-z"). So called "special characters" (Special, because they have another meaning in PHP as well. But that's another story.) have to be escaped, which means you write a backslash in front of it. For example: \- (the hiven) or \. (the dot). Other special characters are: "^[$()|*+?{\".
The other two functions are self explanatory, as they check only for numbers, and numbers and space ("\ ").
Hope this helps.
:mrgreen:
Last edited by assgar; Nov 5th, 2006 at 12:07 am. Reason: spelling
•
•
Join Date: Oct 2009
Posts: 12
Reputation:
Solved Threads: 1
BUMP...
Hi, same problem... I have a piece of code below:
This should check if $new_pagealias & $new_pagekeywords contain invalid characters, other than a-bA-B0-9 etc which works fine.
Well. The first one which checks the page alias works fine. The second one to check the keywords, doesn't. If I post a var to it containing the string '@' it will go through it and enter it into the database, same goes for the string "holiday, sunshine, ' " My HTML page will echo the meta etc ending with: holiday, sunshine, ' '/>
This ends the tag early which will error some browsers, plus I just want it to be perfect and not have to echo invalid keywords.
Plus I need the preg_match for $new_pagekeywords to validate and allow commas.
At the moment if I put the string "My house!" through alias & keywords, it will error for alias, but if I change alias to "house' and leave keywords as "My house!" it won't define $error & stop the SQL input.
Help?
Thanks,
Josh
Hi, same problem... I have a piece of code below:
php Syntax (Toggle Plain Text)
// Check our data if (preg_match('/[^a-zA-Z0-9]/', $new_pagealias)) $error = "Invalid page alias. Use only normal characters."; if (preg_match('/[^a-zA-Z0-9]/', $new_pagekeywords)) $error = "Invalid page keywords. Use only normal characters."; if (!$new_pagealias) $error = "Page alias is missing"; if (!$new_pagename) $error = "Page name is missing"; if (!$new_pagetitle) $error = "Page title is missing";
This should check if $new_pagealias & $new_pagekeywords contain invalid characters, other than a-bA-B0-9 etc which works fine.
Well. The first one which checks the page alias works fine. The second one to check the keywords, doesn't. If I post a var to it containing the string '@' it will go through it and enter it into the database, same goes for the string "holiday, sunshine, ' " My HTML page will echo the meta etc ending with: holiday, sunshine, ' '/>
This ends the tag early which will error some browsers, plus I just want it to be perfect and not have to echo invalid keywords.
Plus I need the preg_match for $new_pagekeywords to validate and allow commas.
At the moment if I put the string "My house!" through alias & keywords, it will error for alias, but if I change alias to "house' and leave keywords as "My house!" it won't define $error & stop the SQL input.
Help?

Thanks,
Josh
![]() |
Similar Threads
- preg_match validation (PHP)
Other Threads in the PHP Forum
- Previous Thread: PHP Parse HTML
- Next Thread: Help for copying images
| Thread Tools | Search this Thread |
301 apache api array autosuggest beginner binary broken cakephp checkbox class cms code compression cron curl data database date display dropdownlist dynamic echo email eregi error execution file files folder foreach form forms function functions google href htaccess html httppost if...loop image include insert ip javascript joomla jquery key library limit link links login mail md5 menu mlm multiple mysql mysql_real_escape_string oop paypal pdf pdfdownload php phpvotingscript problem query radio random recursion remote script search searchbox server session sessions sms sorting source space sql syntax system table tutorial update upload url validator variable video volume votedown web website youtube zend





