preg_match validation help please

Reply

Join Date: Feb 2005
Posts: 4
Reputation: MCAmy is an unknown quantity at this point 
Solved Threads: 0
MCAmy MCAmy is offline Offline
Newbie Poster

preg_match validation help please

 
0
  #1
Mar 1st, 2005
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] ?>
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 348
Reputation: paradox814 is an unknown quantity at this point 
Solved Threads: 4
paradox814's Avatar
paradox814 paradox814 is offline Offline
Posting Whiz

Re: preg_match validation help please

 
0
  #2
Mar 1st, 2005
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]);


  1. //this will seperate every keyword into an array
  2. $keywords = explode("," , $string);
  3.  
  4. //next let's trim any whitespace
  5. for ($i=0; $i<sizeOf($keywords); $i++)
  6. $keywords[$i] = trim($keywords[$i]);
  7.  
  8. $i = 0;
  9. $validKeyWord = true;
  10. while (sizeOf($keywords) < $i && $validKeyWord)
  11. {
  12. if (ereg("^([a-z]+)|[0-9]{2,2})$", $keyword[$i++]))
  13. $validKeyWord= false
  14. }
  15.  
  16. if ($validKeyWord)
  17. {
  18. if (sizeOf($keywords) > 2)
  19. {
  20. echo "error! you are limited to only 2 keywords";
  21. do_something_with_this_function($keywords);
  22. }
  23. else
  24. {
  25. echo "you have 2 keywords or less, good job";
  26. do_whatever_your_site_does_with_keywords($keywords);
  27. }
  28. }
  29. else
  30. {
  31. echo "you did not enter a proper keyword, please try again";
  32. }
Reply With Quote Quick reply to this message  
Join Date: Feb 2005
Posts: 4
Reputation: MCAmy is an unknown quantity at this point 
Solved Threads: 0
MCAmy MCAmy is offline Offline
Newbie Poster

Re: preg_match validation help please

 
0
  #3
Mar 2nd, 2005
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:

  1. <?php
  2. $keywords = "test,green,green,fun test ,trying,work work";
  3.  
  4. //this will seperate every keyword into an array
  5. $keywords = explode("," , $string);
  6.  
  7. //next let's trim any whitespace
  8. for ($i=0; $i<sizeOf($keywords); $i++)
  9. $keywords[$i] = trim($keywords[$i]);
  10.  
  11. $i = 0;
  12. $validKeyWord = true;
  13. while (sizeOf($keywords) < $i && $validKeyWord)
  14. {
  15. if (ereg("^([a-z]+)|[0-9]{2,2})$", $keyword[$i++]))
  16. $validKeyWord= false
  17. }
  18.  
  19. if ($validKeyWord)
  20. {
  21. if (sizeOf($keywords) > 2)
  22. {
  23. echo "error! you are limited to only 2 keywords";
  24. do_something_with_this_function($keywords);
  25. }
  26. else
  27. {
  28. echo "you have 2 keywords or less, good job";
  29. do_whatever_your_site_does_with_keywords($keywords);
  30. }
  31. }
  32. else
  33. {
  34. echo "you did not enter a proper keyword, please try again";
  35. }
  36. ?>
Reply With Quote Quick reply to this message  
Join Date: Feb 2005
Posts: 355
Reputation: DanceInstructor is an unknown quantity at this point 
Solved Threads: 14
DanceInstructor's Avatar
DanceInstructor DanceInstructor is offline Offline
Posting Whiz

Re: preg_match validation help please

 
0
  #4
Mar 2nd, 2005
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
Clear Mind Hosting and Web Design

If I've helped you please consider adding to my reputation.
Reply With Quote Quick reply to this message  
Join Date: Feb 2005
Posts: 4
Reputation: MCAmy is an unknown quantity at this point 
Solved Threads: 0
MCAmy MCAmy is offline Offline
Newbie Poster

Re: preg_match validation help please

 
0
  #5
Mar 2nd, 2005
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.

  1. <?php
  2. $string = "cool,cool too,22";
  3.  
  4. //this will seperate every keyword into an array
  5. $keywords = explode("," , $string);
  6.  
  7. //next let's trim any whitespace
  8. for ($i=0; $i<sizeOf($keywords); $i++)
  9. $keywords[$i] = trim($keywords[$i]);
  10.  
  11. $i = 0;
  12. $validKeyWord = true;
  13. while (sizeOf($keywords) < $i && $validKeyWord)
  14. {
  15. if (ereg("^([a-z]+)|[0-9]{2,2})$", $keyword[$i++]))
  16. $validKeyWord= false;
  17. }
  18.  
  19. if ($validKeyWord)
  20. {
  21. if (sizeOf($keywords) > 3)
  22. {
  23. echo "error! you are limited to only 3 keywords";
  24. ?><br><?php
  25. ?>:<?php echo $keywords[0]; ?>:<br><?php
  26. ?>:<?php echo $keywords[1]; ?>:<br><?php
  27. ?>:<?php echo $keywords[2]; ?>:<br><?php
  28. }
  29. else
  30. {
  31. echo "you have 3 keywords or less, good job";
  32. ?><br><?php
  33. ?>:<?php echo $keywords[0]; ?>:<br><?php
  34. ?>:<?php echo $keywords[1]; ?>:<br><?php
  35. ?>:<?php echo $keywords[2]; ?>:<br><?php
  36. }
  37. }
  38. else
  39. {
  40. echo "you did not enter a proper keyword, please try again";
  41. ?><br><?php
  42. ?>:<?php echo $keywords[0]; ?>:<br><?php
  43. ?>:<?php echo $keywords[1]; ?>:<br><?php
  44. ?>:<?php echo $keywords[2]; ?>:<br><?php
  45. }
  46. ?>
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 82
Reputation: assgar is an unknown quantity at this point 
Solved Threads: 0
assgar assgar is offline Offline
Junior Poster in Training

Re: preg_match validation help please

 
0
  #6
Nov 5th, 2006
Originally Posted by MCAmy View Post
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] ?>
Hi

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
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 12
Reputation: rokape is an unknown quantity at this point 
Solved Threads: 1
rokape rokape is offline Offline
Newbie Poster

preg_match errors

 
-1
  #7
Oct 25th, 2009
BUMP...

Hi, same problem... I have a piece of code below:

  1. // Check our data
  2. if (preg_match('/[^a-zA-Z0-9]/', $new_pagealias)) $error = "Invalid page alias. Use only normal characters.";
  3. if (preg_match('/[^a-zA-Z0-9]/', $new_pagekeywords)) $error = "Invalid page keywords. Use only normal characters.";
  4. if (!$new_pagealias) $error = "Page alias is missing";
  5. if (!$new_pagename) $error = "Page name is missing";
  6. 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
Reply With Quote Quick reply to this message  
Reply

Message:



Similar Threads
Other Threads in the PHP Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC