This is a discussion on Regular expressions within the alt.comp.lang.php forums, part of the PHP Programming Forums category; Hi, I need one function to validate streetnames and one to validate names. I tried to write my own regular ...
|
|||||||
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
|
|||
|
Hi,
I need one function to validate streetnames and one to validate names. I tried to write my own regular expression and to use preg_match, but for some weird reason I can't get it to work. Valid names should be: Peter Christian-Peter Li Li Yan Young Li - Yan - Young Li- Yan -Young - Mei Valid streetnames should be: Jackrippperstr. 10a Jackrippperstr 9391 B Jack-The - Ripper Str. 3 Jack -The- Ripper B Jack -The- Ripper Here are my regular expressions: ++++++++++++++++++++++++++++++++++++++++++++++++ /*///////////// ///1. Names /// /////////////*/ //At least 2 chars: $twochars ="(A-Za-zίδόφΔάΦ){2,}"; //Including german special chars // Separator with space and or - $separator= "([:space:]?-[:space:]?|[:space:])"; /* (Name consists of at least 2 chars, optionally followed by one space char and or - , followed by again at least 2 chars )+ */ $name= "($twochars($separator$twochars)*)+"; if ( preg_match("/$name/",$value) ) ++++++++++++++++++++++++++++++++++++++++++++++++ /*/////////////////////////// ///2. Streetname + number /// ///////////////////////////*/ //At least 2 chars: $twochars ="(A-Za-zίδόφΔάΦ){2,}"; //Including german special chars // Separator with space and or - $separator= "([:space:]?-[:space:]?|[:space:])"; // optionally Str. or Str or str or Str. $str = "([Ss]tr|[Ss]tr.)?"; /* The numbers (1-9 followed by 0-9 ) - zero or four times, followed by space or not, optionally followed by a character. */ $nr = "((1-9)+(0-9)*){0,4}[:space:]?(A-Za-z)?"; $street= "($twochars($separator$twochars)*)+$str$nr"; if ( preg_match("/$street/",$value) ) ... ++++++++++++++++++++++++++++++++++++++++++++++++ Have you any idea why this doesn't work? Thanks, Stefan |
|
|||
|
....
> Hi, > > I need one function to validate streetnames and one to validate names. > I tried to write my own regular expression and to use preg_match, > but for some weird reason I can't get it to work. > > Valid names should be: > Peter > Christian-Peter > Li > Li Yan Young > Li - Yan - Young > Li- Yan -Young - Mei > > Valid streetnames should be: > Jackrippperstr. 10a > Jackrippperstr 9391 B > Jack-The - Ripper Str. 3 > Jack -The- Ripper B > Jack -The- Ripper > > Here are my regular expressions: > ++++++++++++++++++++++++++++++++++++++++++++++++ > /*///////////// > ///1. Names /// > /////////////*/ > > //At least 2 chars: > $twochars ="(A-Za-zίδόφΔάΦ){2,}"; //Including german special chars > // Separator with space and or - > $separator= "([:space:]?-[:space:]?|[:space:])"; > /* > (Name consists of at least 2 chars, optionally followed by one space char > and or - , followed by again at least 2 chars )+ > */ > $name= "($twochars($separator$twochars)*)+"; > if ( preg_match("/$name/",$value) ) First your $twochars should be a caracter class "[A-Za-zίδόφΔάΦ]{2,}" not a capturing parenthese. For the rest, you're making things far too complicated for nothing (IMHO). Here is something simple that should work : $twochars ="[A-Za-zίδόφΔάΦ]{2,}"; $separator= "[\s\-]+"; $name ="/$twochars($separator$twochars)*/"; if ( preg_match($name,$value)) Try it and take it as a starting point to correct the street regexp Dae > ++++++++++++++++++++++++++++++++++++++++++++++++ > /*/////////////////////////// > ///2. Streetname + number /// > ///////////////////////////*/ > > > //At least 2 chars: > $twochars ="(A-Za-zίδόφΔάΦ){2,}"; //Including german special chars > // Separator with space and or - > $separator= "([:space:]?-[:space:]?|[:space:])"; > // optionally Str. or Str or str or Str. > $str = "([Ss]tr|[Ss]tr.)?"; > /* > The numbers (1-9 followed by 0-9 ) - zero or four times, followed by space > or not, optionally followed by a character. > */ > $nr = "((1-9)+(0-9)*){0,4}[:space:]?(A-Za-z)?"; > > $street= "($twochars($separator$twochars)*)+$str$nr"; > if ( preg_match("/$street/",$value) ) ... > ++++++++++++++++++++++++++++++++++++++++++++++++ > > Have you any idea why this doesn't work? > > Thanks, > > Stefan |
|
|||
|
Usually you don't need to be so precise about address verification ,unless
you have very specific reasons to do so. All you want to do is to untaint the user entry. The fact is that even with strong "formatting" verification, the user can always enter invalid addresses in a valid format. Basically I would untaint address with a simple character class, by specifying what characters are allowed. For exemple the name regexp: $name = '/[a-z\s\-]{2,}/i' // This will allow any letters, space and minus sign. At least 2 character should be use and the regexp will be insensitive to case. Basically I would use the same regexp for country. Just add the german or any other language specific letters. The point "." represent any character (except new lines and possibly some other non-printed characters). so this: '/.+/'; would mean any character, 1 or more times. But this will not really untaint the user entry. Hope this help Dae "Stefan Reiter" <Dooing_DONTSPAMME@gmx.de> wrote in message news:425018a0$0$27200$9b4e6d93@newsread4.arcor-online.net... > Any idea for a regular expression including all chars (all country > specific letters)??? > > Thanks, > > Stefan |