[PHP] FormFieldChecker

Pagina: 1
Acties:

  • vinnux
  • Registratie: Maart 2001
  • Niet online
Nu ik al een hele tijd bezgi ben met PHP en vaak met Form velden zit te werken en te kijken of hun waarde mogen heb ik de volgende functies gemaakt :

isUrl()
Controleert of een waarde voldoet aan de staandaard van een url, beginned met http:// (als niet aanwezig voet ie die toe) gevold door minimaal 3 tekens waarvan 1 een punt. En natuurlijke alle speciale tekens die er niet in mogen worden ook gecheckt. Tevens kun je opgeven wat de minimale en maximale lengte moet zijn.

isIPV4()
Controleert of een waarde voldoet aan de IPV4 standaard, volgends format 192.67.34.5 Alle seperate waarden binnen de [0-255] range

isTime
Controleert of een waarde een tijd zou kunnen zijn. Als seperators mag alles gebruikt worden als er maar drie getallen inzitten die uiteindelijk een geldige uur:min:seconden combinatie zijn. (23fsfsd/fsd44:45 is dus een geldige)

isDate
Zelfde als isTime alleen dan volgend formaat dag, maand, jaar.
edit:
Checkt ook of het een geldige datum is


isDateTime
Combinatie van isTime en isDate, volgens formaar dag, maand, jaar uur,minuut,seconde.

isEmailAdress
Controleert of een email valide is. Ook kun je opgeven wat de minimale en maximale lengte is.

isOnlyNumber()
Controleert of er alleen maar nummers in een waarde zitten en eventueel of ie binnen bepaalde waarden ligt.

Leuk he :)

Nou is mijn vraag mis ik nog zaken die er zeker in zouden moeten ?

Let op : Het gaat hier om het controleren van data dat van een HTML Form afkomt.

  • Postman
  • Registratie: Februari 2000
  • Laatst online: 00:58
isCountry, isStreet, isName (<- goeie deze :))
Die date en time functie, checken die ook of het een geldige datum is (data is het verleden zijn meestal niet geldig :+)

  • vinnux
  • Registratie: Maart 2001
  • Niet online
Op dinsdag 16 april 2002 01:57 schreef FlamerX het volgende:
isCountry, isStreet, isName (<- goeie deze :))
Die date en time functie, checken die ook of het een geldige datum is (data is het verleden zijn meestal niet geldig :+)
isCountry
Geef maar een lijst met alle landen in verschillende talen. Een url vind ik ook goed.

De andere zijn een beetje onzinnig denk ik, maar overtuig me en ik maak ze en post de code.

  • sjroorda
  • Registratie: December 2001
  • Laatst online: 09:21
Op dinsdag 16 april 2002 02:22 schreef vgouw het volgende:
isCountry
Geef maar een lijst met alle landen in verschillende talen. Een url vind ik ook goed.
U vraagt, wij draaien ;) Hierbij een lijst met landnamen in het Engels. Vertalen moet je zelf maar doen. Kan je makkelijk gebruiken in select-dingetje (daar komt het ook vandaan)
PHP:
1
<?    $cntry["NL"] = "Netherlands";    $cntry["BE"] = "Belgium";    $cntry["AF"] = "Afghanistan";    $cntry["AL"] = "Albania";    $cntry["DZ"] = "Algeria";    $cntry["AS"] = "American Samoa";    $cntry["AD"] = "Andorra";    $cntry["AO"] = "Angola";    $cntry["AI"] = "Anguilla";    $cntry["AQ"] = "Antarctica";    $cntry["AG"] = "Antigua and Barbuda";    $cntry["AR"] = "Argentina";    $cntry["AM"] = "Armenia";    $cntry["AW"] = "Aruba";    $cntry["AU"] = "Australia";    $cntry["AT"] = "Austria";    $cntry["AZ"] = "Azerbaijan";    $cntry["BS"] = "Bahamas";    $cntry["BH"] = "Bahrain";    $cntry["BD"] = "Bangladesh";    $cntry["BB"] = "Barbados";    $cntry["BY"] = "Belarus";    $cntry["BZ"] = "Belize";    $cntry["BJ"] = "Benin";    $cntry["BM"] = "Bermuda";    $cntry["BT"] = "Bhutan";    $cntry["BO"] = "Bolivia";    $cntry["BA"] = "Bosnia and Herzegowina";    $cntry["BW"] = "Botswana";    $cntry["BV"] = "Bouvet Island";    $cntry["BR"] = "Brazil";    $cntry["IO"] = "British Indian Ocean Territory";    $cntry["BN"] = "Brunei Darussalam";    $cntry["BG"] = "Bulgaria";    $cntry["BI"] = "Burundi";    $cntry["KH"] = "Cambodia";    $cntry["CM"] = "Cameroon";    $cntry["CA"] = "Canada";    $cntry["CV"] = "Cape Verde";    $cntry["KY"] = "Cayman Islands";    $cntry["CF"] = "Central African Republic";    $cntry["TD"] = "Chad";    $cntry["CL"] = "Chile";    $cntry["CN"] = "China";    $cntry["CX"] = "Christmas Island";    $cntry["CC"] = "Cocos (Keeling) Islands";    $cntry["CO"] = "Colombia";    $cntry["KM"] = "Comoros";    $cntry["CG"] = "Congo";    $cntry["CK"] = "Cook Islands";    $cntry["CR"] = "Costa Rica";    $cntry["CI"] = "Cote D'Ivoire";    $cntry["HR"] = "Croatia";    $cntry["CU"] = "Cuba";    $cntry["CY"] = "Cyprus";    $cntry["CZ"] = "Czech Republic";    $cntry["DK"] = "Denmark";    $cntry["DJ"] = "Djibouti";    $cntry["DM"] = "Dominica";    $cntry["DO"] = "Dominican Republic";    $cntry["TP"] = "East Timor";    $cntry["EC"] = "Ecuador";    $cntry["EG"] = "Egypt";    $cntry["SV"] = "El Salvador";    $cntry["GQ"] = "Equatorial Guinea";    $cntry["ER"] = "Eritrea";    $cntry["EE"] = "Estonia";    $cntry["ET"] = "Ethiopia";    $cntry["FK"] = "Falkland Islands";    $cntry["FO"] = "Faroe Islands";    $cntry["FJ"] = "Fiji";    $cntry["FI"] = "Finland";    $cntry["FR"] = "France";    $cntry["FX"] = "France (Metropolitan)";    $cntry["GF"] = "French Guiana";    $cntry["PF"] = "French Polynesia";    $cntry["TF"] = "French Southern Territories";    $cntry["GA"] = "Gabon";    $cntry["GM"] = "Gambia";    $cntry["GE"] = "Georgia";    $cntry["DE"] = "Germany";    $cntry["GH"] = "Ghana";    $cntry["GI"] = "Gibraltar";    $cntry["GR"] = "Greece";    $cntry["GL"] = "Greenland";    $cntry["GD"] = "Grenada";    $cntry["GP"] = "Guadeloupe";    $cntry["GU"] = "Guam";    $cntry["GT"] = "Guatemala";    $cntry["GN"] = "Guinea";    $cntry["GW"] = "Guinea-Bissau";    $cntry["GY"] = "Guyana";    $cntry["HT"] = "Haiti";    $cntry["HM"] = "Heard and McDonald Islands";    $cntry["HN"] = "Honduras";    $cntry["HK"] = "Hong Kong";    $cntry["HU"] = "Hungary";    $cntry["IS"] = "Iceland";    $cntry["IN"] = "India";    $cntry["ID"] = "Indonesia";    $cntry["IR"] = "Iran";    $cntry["IQ"] = "Iraq";    $cntry["IE"] = "Ireland";    $cntry["IL"] = "Israel";    $cntry["IT"] = "Italy";    $cntry["JM"] = "Jamaica";    $cntry["JP"] = "Japan";    $cntry["JO"] = "Jordan";    $cntry["KZ"] = "Kazakhstan";    $cntry["KE"] = "Kenya";    $cntry["KI"] = "Kiribati";    $cntry["KP"] = "Korea (Democratic People's Republic of)";    $cntry["KR"] = "Korea (Republic of)";    $cntry["KW"] = "Kuwait";    $cntry["KG"] = "Kyrgyzstan";    $cntry["LA"] = "Lao People's Democratic Republic";    $cntry["LV"] = "Latvia";    $cntry["LB"] = "Lebanon";    $cntry["LS"] = "Lesotho";    $cntry["LR"] = "Liberia";    $cntry["LY"] = "Libyan Arab Jamahiriya";    $cntry["LI"] = "Liechtenstein";    $cntry["LT"] = "Lithuania";    $cntry["LU"] = "Luxembourg";    $cntry["MO"] = "Macau";    $cntry["MK"] = "Macedonia (The Former Yugoslav Republic of)";    $cntry["MG"] = "Madagascar";    $cntry["MW"] = "Malawi";    $cntry["MY"] = "Malaysia";    $cntry["MV"] = "Maldives";    $cntry["ML"] = "Mali";    $cntry["MT"] = "Malta";    $cntry["MH"] = "Marshall Islands";    $cntry["MQ"] = "Martinique";    $cntry["MR"] = "Mauritania";    $cntry["MU"] = "Mauritius";    $cntry["YT"] = "Mayotte";    $cntry["MX"] = "Mexico";    $cntry["FM"] = "Micronesia (Federated States of)";    $cntry["MD"] = "Moldova (Republic of)";    $cntry["MC"] = "Monaco";    $cntry["MN"] = "Mongolia";    $cntry["MS"] = "Montserrat";    $cntry["MA"] = "Morocco";    $cntry["MZ"] = "Mozambique";    $cntry["MM"] = "Myanmar";    $cntry["NA"] = "Namibia";    $cntry["NR"] = "Nauru";    $cntry["NP"] = "Nepal";    $cntry["AN"] = "Netherlands Antilles";    $cntry["NC"] = "New Caledonia";    $cntry["NZ"] = "New Zealand";    $cntry["NI"] = "Nicaragua";    $cntry["NE"] = "Niger";    $cntry["NG"] = "Nigeria";    $cntry["NU"] = "Niue";    $cntry["NF"] = "Norfolk Island";    $cntry["MP"] = "Northern Mariana Islands";    $cntry["NO"] = "Norway";    $cntry["OM"] = "Oman";    $cntry["PK"] = "Pakistan";    $cntry["PW"] = "Palau";    $cntry["PA"] = "Panama";    $cntry["PG"] = "Papua New Guinea";    $cntry["PY"] = "Paraguay";    $cntry["PE"] = "Peru";    $cntry["PH"] = "Philippines";    $cntry["PN"] = "Pitcairn";    $cntry["PL"] = "Poland";    $cntry["PT"] = "Portugal";    $cntry["PR"] = "Puerto Rico";    $cntry["QA"] = "Qatar";    $cntry["RE"] = "Reunion";    $cntry["RO"] = "Romania";    $cntry["RU"] = "Russian Federation";    $cntry["RW"] = "Rwanda";    $cntry["KN"] = "Saint Kitts and Nevis";    $cntry["LC"] = "Saint Lucia";    $cntry["VC"] = "Saint Vincent and the Grenadines";    $cntry["WS"] = "Samoa";    $cntry["SM"] = "San Marino";    $cntry["ST"] = "Sao Tome and Principe";    $cntry["SA"] = "Saudi Arabia";    $cntry["SN"] = "Senegal";    $cntry["SC"] = "Seychelles";    $cntry["SL"] = "Sierra Leone";    $cntry["SG"] = "Singapore";    $cntry["SK"] = "Slovakia (Slovak Republic)";    $cntry["SI"] = "Slovenia";    $cntry["SB"] = "Solomon Islands";    $cntry["SO"] = "Somalia";    $cntry["ZA"] = "South Africa";    $cntry["GS"] = "South Georgia and the South Sandwich Islands";    $cntry["ES"] = "Spain";    $cntry["LK"] = "Sri Lanka";    $cntry["SH"] = "St. Helena";    $cntry["PM"] = "St. Pierre and Miquelon";    $cntry["SD"] = "Sudan";    $cntry["SR"] = "Suriname";    $cntry["SJ"] = "Svalbard and Jan Mayen Islands";    $cntry["SZ"] = "Swaziland";    $cntry["SE"] = "Sweden";    $cntry["CH"] = "Switzerland";    $cntry["SY"] = "Syrian Arab Republic";    $cntry["TW"] = "Taiwan (Province of China)";    $cntry["TJ"] = "Tajikistan";    $cntry["TZ"] = "Tanzania (United Republic of)";    $cntry["TH"] = "Thailand";    $cntry["TG"] = "Togo";    $cntry["TK"] = "Tokelau";    $cntry["TO"] = "Tonga";    $cntry["TT"] = "Trinidad and Tobago";    $cntry["TN"] = "Tunisia";    $cntry["TR"] = "Turkey";    $cntry["TM"] = "Turkmenistan";    $cntry["TC"] = "Turks and Caicos Islands";    $cntry["TV"] = "Tuvalu";    $cntry["UG"] = "Uganda";    $cntry["UA"] = "Ukraine";    $cntry["AE"] = "United Arab Emirates";    $cntry["GB"] = "United Kingdom";    $cntry["UM"] = "United States Minor Outlying Islands";    $cntry["US"] = "United States of America";    $cntry["UY"] = "Uruguay";    $cntry["UZ"] = "Uzbekistan";    $cntry["VU"] = "Vanuatu";    $cntry["VA"] = "Vatican City State (Holy See)";    $cntry["VE"] = "Venezuela";    $cntry["VN"] = "Viet Nam";    $cntry["VG"] = "Virgin Islands (British)";    $cntry["VI"] = "Virgin Islands (U.S.)";    $cntry["WF"] = "Wallis and Futuna Islands";    $cntry["EH"] = "Western Sahara";    $cntry["YE"] = "Yemen";    $cntry["YU"] = "Yugoslavia";    $cntry["ZR"] = "Zaire";    $cntry["ZM"] = "Zambia";    $cntry["ZW"] = "Zimbabwe";?>

Verwijderd

isNaam()
Controleerd of een geldige naam is ingevoerd (kun je bv. bepaalde namen wijgeren (als: root, http, pid, eigen naam, k@t, L@l, enz)

isTekst()
Controleren of de tekst die is ingevoed geldig is. Dus niet te veel hoofdletters bevat, niet te veel smilies bevat, enz.

  • Dennis
  • Registratie: Februari 2001
  • Laatst online: 13:59
Als je dit nou in een leuke class stopt, heb je er zeker heel veel aan ja! :)

  • HenkS
  • Registratie: Mei 2000
  • Laatst online: 09-09 22:53

HenkS

Da_king alias HenkS

isBanned()

kijken of poster niet voorkomt in banlist...

  • Glimi
  • Registratie: Augustus 2000
  • Niet online

Glimi

Designer Drugs

(overleden)
Uhm het lijkt mij gewoon dat je alles controleerd wat via userinput binnenkomt (zelfs hidden vars oid). _Vertrouw_ nooit een user!! Met mijn minimale C++/Java kennis kan ik al een socket open trekken, HTTP headers sturen en alles invoeren wat ik wil.

Kortom, voor elk type veld dat je tot nu toe hebt, moet je een controle maken. Zou ik een mooi object voor maken :)

  • BierPul
  • Registratie: Juni 2001
  • Laatst online: 09-09 23:24

BierPul

2 koffie graag

offtopic:
isLekkerWijf()
Verdent extra aandacht :P


:D

Ja man


  • vinnux
  • Registratie: Maart 2001
  • Niet online
Nou het zit in een clase hoor. Iemand interesees in de code ?

  • drm
  • Registratie: Februari 2001
  • Laatst online: 09-06-2025

drm

f0pc0dert

't mooiste is natuurlijk als je een klasse hebt die generiek de formuliertjes genereert en ook automatisch controleert...

Music is the pleasure the human mind experiences from counting without being aware that it is counting
~ Gottfried Leibniz


  • Grum
  • Registratie: Juni 2001
  • Niet online
Nu we het toch over regexps hebben ... hoe ziet je isIPV4 functie eruit ? :)

edit:
toch wel een regex he :? :+

  • vinnux
  • Registratie: Maart 2001
  • Niet online
Op dinsdag 16 april 2002 09:23 schreef Grum het volgende:
Nu we het toch over regexps hebben ... hoe ziet je isIPV4 functie eruit ? :)

edit:
toch wel een regex he :? :+
Hier de klasse :
PHP:
1
<?class FormFieldChecker{    function FormFieldChecker(){        }    /**     * Checks if the given url matches de description of a url.     * If the url is no started with http:// it is added before the check.     * Url has to start with http://, followed by something with length at least 3 that     * contains at least one dot.     **/    function isURL($url){        $isUrl = FALSE;        if ( $url != '' ){            if (!preg_match('#^http:\/\/#i', $url) ){                $url = 'http://' . $url;            }            if (preg_match('#^http\\:\\/\\/[a-z0-9\-]+\.([a-z0-9\-]+\.)?[a-z]+#i', $url) ){                $isURL = TRUE;            }        }        return $isURL;    }        /**      * Checks to see if the given numberString only contains numbers      **/    function isOnlyNumbers($numberString) {        $isOnlyNumbers = FALSE;        if ( preg_match('/^[0-9]+$/', $numberString) ){            $isOnlyNumbers = TRUE;        }        return $isOnlyNumbers;    }            /**     * Checks if the email is a valide one     **/    function isEmailAdress($email){        $isEmailAdress = FALSE;        if ( $email != '' )    {            if ( preg_match('/^[a-z0-9\.\-_]+@[a-z0-9\-_]+\.([a-z0-9\-_]+\.)*?[a-z]+$/is', $email) ){                $isEmailAdress = TRUE;            }        }        return $isEmailAdress;    }        /**     * Checks if an $IP is valid, according to 192.168.168.20     **/    function isIPV4($IP){        $isIP = FALSE;        if ($IP != ''){            $parts = explode('.',$IP);            if (count($parts) == 4){                $partsOk = 0;                for ($nr = 0 ; $nr < 4 ; $nr++){                    if ($this -> isOnlyNumbers($parts[$nr]) &amp;&amp; $parts[$nr] >= 0 &amp;&amp; $parts[$nr] <= 255 ){                        $partsOk++;                    } else {                        break;                    }                }                if ($partsOk === 4){                    $isIP = TRUE;                }            }        }        return $isIP;    }        /**     * Check to see if the $time is a valid time. Seperator kan be everythint but number ans as many as possible     * 23fdfdfas12fsdfsdfsdf6 is a valid time     **/    function isTime($time){        $isTime = FALSE;        if ($time != ''){            $parts = split('[^[:digit:]]+',$time) ;            if (count($parts) === 3){                if (   $parts[0] >= 0 &amp;&amp; $parts[0] <= 24                     &amp;&amp; $parts[1] >= 0 &amp;&amp; $parts[1] <= 59                     &amp;&amp; $parts[2] >= 0 &amp;&amp; $parts[2] <= 59 ){                    $isTime = TRUE;                }            }        }        return $isTime;    }        /**     * Checks is a date if valid. Seperator can be everything except numbers.      * The given date must complie with the format : day , month, year     **/    function isDate($date){        $isDate = FALSE;        if ($date != ''){            $parts = split('[^[:digit:]]+',$time) ;            if (count($parts) === 3){                    $isDate    = checkdate($parts[0],$parts[1],$parts[2]);         }        }        return $isDate;    }        /**     * Checks is a dateTime if valid. Seperator can be everything except numbers.      * The given date must complie with the format : day , month, year, hour,minute,second     **/    function isDateTime($dateTime){        $isDateTime = FALSE;        if ($dateTime != ''){            $parts = split('[^[:digit:]]+',$dateTime);            if (count($parts === 6)){                if ($this -> isDate($parts[0].'-'.$parts[1].'-',$parts[2]) &amp;&amp; isTime($parts[3].':'.$parts[4].':',$parts[5]) ){                    $isDateTime = TRUE;                }            }        }        return $isDateTime;     }        /**      *    Function which checks the form fields, according to their types.     *  Supported are : EMAIL, IPV4, TIME, DATE, DATETIME, INT, STRING     *      */    function isValidFormValue($value,$type = 'STRING',$min = FALSE, $max = FALSE){        $type = strtoupper($type);        // Checks for the types STRING, EMAIL, URL if the lenght is within margines, if given.        if (   $type == 'STRING'             || $type !== 'EMAIL'             || $type !== 'URL'      ){            if ($min !== FALSE &amp;&amp; strlen($value) < $min){                return FALSE;            }            if ($max !== FALSE &amp;&amp; strlen($value) > $max){                return FALSE;            }        }        // Checks for the different types        if ($type === 'EMAIL'){            return $this -> isEmailAdress($value);        } else if ($type === 'URL'){            return $this -> isUrl($value);        } else if ($type === 'IPV4'){            return $this -> isIPV4($value);        } else if ($type === 'TIME') {            return $this -> isTime($value);        } else if ($type === 'DATE') {            return $this -> isDate($value);        } else if ($type === 'DATETIME') {            return $this -> isDateTime($value);        } else if ($type === 'INT') {            $isOnlyNumbers = FALSE;            if ($this -> isOnlyNumbers($value)){                 $isOnlyNumbers = TRUE;                if ($min !== FALSE &amp;&amp; $this -> isOnlyNumbers($min) &amp;&amp; $value >= $min){                   $isOnlyNumbers = TRUE;                }                 if ($max !== FALSE &amp;&amp; $this -> isOnlyNumbers($max) &amp;&amp; $value <= $max){                     $isOnlyNumbers = TRUE;                }             }            return     $isOnlyNumbers;         } else if ($type === 'STRING') {            $isString = TRUE;            return $isString;        }        return FALSE;    }}?>

Met regulire expressies snapt ik nog wel hoe ik het patroon moet controleren. Maar hoe ik controleer dat vervolgens de individuele stukje tussen de 0 en 255 waren, daar kwam ik toch niet achter.

Maar misschien als ik wat meer lees komt het vanzelf !

  • HenkS
  • Registratie: Mei 2000
  • Laatst online: 09-09 22:53

HenkS

Da_king alias HenkS

ik zou zeggen gooi die class maar eens hier neer ja...

  • Bosmonster
  • Registratie: Juni 2001
  • Laatst online: 29-08 19:47

Bosmonster

*zucht*

isPostCode()
isXWords(x)
isMandatory() (duh!)

  • vinnux
  • Registratie: Maart 2001
  • Niet online
Op dinsdag 16 april 2002 09:31 schreef Bosmonster het volgende:
isPostCode()
isXWords(x)
isMandatory() (duh!)
Ja dat zijn goede, ff er bij zetten.

  • Grum
  • Registratie: Juni 2001
  • Niet online
Ik heb eventjes de functie in jouw codestyle neergezet en zal zometeen eventjes de regexp uitschrijven met wat uitleg erbij.
PHP:
1
<?function isIPV4($inIp){   if(preg_match('!^(\d|[1-9]\d|1\d{2}|2[0-4]\d|25[0-5])(\.(\d|[1-9]\d|1\d{2}|2[0-4]\d|25[0-5])){3}$!', $inIp) == false)      return TRUE;    return FALSE;}?>


PHP:
1
<?function isIPV4($inIp){   $thePattern = '!^    # begin of line   (                    # match 0-255      \d                # 1-9   |      [1-9]\d           # 10-99   |      1\d{2}            # 100-199   |      2[0-4]\d          # 200-249   |      25[0-5]           # 250-255   )   (      \.                # literal dot      (                 # same as above but without whitespace         \d|[1-9]\d|    # and comments         1\d{2}|         2[0-4]\d|         25[0-5]      )   ){3}                 # exactly 3 times   $!x';                # end of line                        # ignore whitespace/comments modifier    if(preg_match($thePattern, $inIp) == false)      return FALSE;    return TRUE;}?>

  • vinnux
  • Registratie: Maart 2001
  • Niet online
Nou het begint te werken die reguliere expressie zie hier
PHP:
1
<?function isDutchPostcode($postcode){        $isDutchPostcode = FALSE;        if ($postcode != ''){            if (preg_match('/^[0-9]{4} ?[a-zA-Z]{2}$/',$postcode))            $isDutchPostcode = TRUE;        }        return $isDutchPostcode;    }?>

De reguliere expressie controleert of er begonnen wordt met 4 cijfers, dan eventueel een spatie, en daarna 2 letters uppercase en lowercase.

En GRUM, bedankt voor de uitleg, die ging nog ff mijn petje te boven hoor, die regex voor ipv4.

[ Voor 4% gewijzigd door vinnux op 24-09-2003 23:40 ]


  • Grum
  • Registratie: Juni 2001
  • Niet online
[edit]Tnx ACM :*[/edit]
[edit2]Niet met een nul beginnen maar wel erin zitten :D[/edit2]

Je kan beter dit als pattern gebruiken:
code:
1
/^[1-9]\d{3}\s*[a-z]{2}$/i

\d = [0-9]
\s = all whitespace chars
/i = ignore case

[edit]
vgouw: En GRUM, bedankt voor de uitleg, die ging nog ff mijn petje te boven hoor, die regex voor ipv4.

Het probleem is niet em te lezen maar em te bedenken.

Het is een enorme ROT regexp waar je enorm goed moet bijhouden of je geen kleine stukjes vergeet. Als je hem maakt zoals ik em nu heb uitgeschreven dan is het echter wel gewoon te doen.

  • ACM
  • Registratie: Januari 2000
  • Niet online

ACM

Software Architect

Werkt hier

Op dinsdag 16 april 2002 09:39 schreef Grum het volgende:
Ik heb eventjes de functie in jouw codestyle neergezet en zal zometeen eventjes de regexp uitschrijven met wat uitleg erbij.
Je commentaar klopt niet helemaal ;) \d matched toch 0-9 ipv 1-9? :)
Op dinsdag 16 april 2002 09:57 schreef vgouw het volgende:
De reguliere expressie controleert of er begonnen wordt met 4 cijfers, dan eventueel een spatie, en daarna 2 letters uppercase en lowercase.
Volgens mij mag een postcode niet met 0 beginnen ;)

  • vinnux
  • Registratie: Maart 2001
  • Niet online
Op dinsdag 16 april 2002 09:59 schreef Grum het volgende:
Je kan beter dit als pattern gebruiken:
code:
1
/^\d{4}\s*[a-z]{2}$/i

\d = [0-9]
\s = all whitespace chars
/i = ignore case
Ik zie het al weer, er leiden meerde wegen naar rome. Bedankt voor de uitleg over \d, \s en /i.

  • Bosmonster
  • Registratie: Juni 2001
  • Laatst online: 29-08 19:47

Bosmonster

*zucht*

isDutchTelephone()

(10 cijfers)

En vergeet natuurlijk niet de overige form-elementen als checkboxes, radiobuttons, selectboxes, etc etc ;)

  • vinnux
  • Registratie: Maart 2001
  • Niet online
Op dinsdag 16 april 2002 10:04 schreef Bosmonster het volgende:
isDutchTelephone()

(10 cijfers)
Dat gaat iets worden in de trent van :

Begint met 00
Begint met +
begint met + en zit (d) int
of gewoon 10 cijfers

Maar land dingetje zoal 0031 zijn die altijd 4 lang. En mocht iemd het toevallig weten, maar ik zoek ook wel fff :), een lijst met alle landen nummers

  • Grum
  • Registratie: Juni 2001
  • Niet online
Is telefoon niet (voor nederland iig):

+31[1-9][0-9]{9}
of
0031[1-9][0-9]{9}
of
0[1-9][0-9]{9}

  • GraasGast
  • Registratie: Oktober 2000
  • Laatst online: 03-09 17:11

GraasGast

Analogue Heaven

netjes hoor :)

alleen je functies kunnen wat beknopter/sneller...
dus zo:
PHP:
1
<?function isDutchPostcode($postcode) {    return preg_match('/^[0-9]{4} ?[a-zA-Z]{2}$/',$postcode) ? TRUE : FALSE;}?>

ipv zo:
PHP:
1
<?function isDutchPostcode($postcode) {    $isDutchPostcode = FALSE;    if ($postcode != '') {        if (preg_match('/^[0-9]{4} ?[a-zA-Z]{2}$/',$postcode))            $isDutchPostcode = TRUE;    }    return $isDutchPostcode;}?>

  • vinnux
  • Registratie: Maart 2001
  • Niet online
Op dinsdag 16 april 2002 10:12 schreef Grum het volgende:
Is telefoon niet (voor nederland iig):

+31[1-9][0-9]{9}
of
0031[1-9][0-9]{9}
of
0[1-9][0-9]{9}
Je was me net voor !
Maar deze \+31\(d\)[0-9]{9}
Die moest er ook nog bij.

  • ACM
  • Registratie: Januari 2000
  • Niet online

ACM

Software Architect

Werkt hier

Op dinsdag 16 april 2002 10:14 schreef GraasGast het volgende:
alleen je functies kunnen wat beknopter/sneller...
dus zo:
Wordt ie niks sneller van en alleen maar minder leesbaar ;)

Btw, ook jij staat toe dat er postcodes met 0 beginnen, de TNT-postgroup volgens mij niet ;)

  • GraasGast
  • Registratie: Oktober 2000
  • Laatst online: 03-09 17:11

GraasGast

Analogue Heaven

Op dinsdag 16 april 2002 10:19 schreef ACM het volgende:

[..]

Wordt ie niks sneller van en alleen maar minder leesbaar ;)

Btw, ook jij staat toe dat er postcodes met 0 beginnen, de TNT-postgroup volgens mij niet ;)
Ok...als jij het zegt...

hey, ik heb die regex niet gemaakt! :P

edit:
code:
1
'/^[1-9][0-9]{3} ?[a-zA-Z]{2}$/

Beter? :)

  • Grum
  • Registratie: Juni 2001
  • Niet online
GraasGast: hey, ik heb die regex niet gemaakt! :P


Nou en ? .. als je de thread had gelezen had je wel de 'juiste' gezien :+

edit:
Nee GraasGast .. je lees de thread niet :D

  • ACM
  • Registratie: Januari 2000
  • Niet online

ACM

Software Architect

Werkt hier

Op dinsdag 16 april 2002 10:19 schreef GraasGast het volgende:
hey, ik heb die regex niet gemaakt! :P
Noem het dan geen verbetering :P
Ow, btw, jouw functie is vaak zelfs iets trager gok ik, vooral als de postcode leeg is (waar de empty() functie voor bedacht is)

  • Grum
  • Registratie: Juni 2001
  • Niet online
Ik vraag me af wat sneller is

Empty icm preg of alleen preg

Het gros van de tijd staat er namelijk WEL wat in en dan is de empty overbodig en de check failed toch op de preg als ie een leeg veld krijgt :)

  • ACM
  • Registratie: Januari 2000
  • Niet online

ACM

Software Architect

Werkt hier

Op dinsdag 16 april 2002 10:23 schreef Grum het volgende:
Het gros van de tijd staat er namelijk WEL wat in en dan is de empty overbodig en de check failed toch op de preg als ie een leeg veld krijgt :)
Het laden van die preg-engine is best wel veel werk :)
Magoed, echt boeiend is het verschil niet en dan zou ik voor functie lees- en uitbreidbaarheid gaan.

  • GraasGast
  • Registratie: Oktober 2000
  • Laatst online: 03-09 17:11

GraasGast

Analogue Heaven

Op dinsdag 16 april 2002 10:21 schreef ACM het volgende:

[..]

Noem het dan geen verbetering :P
Ow, btw, jouw functie is vaak zelfs iets trager gok ik, vooral als de postcode leeg is (waar de empty() functie voor bedacht is)
ik had op de if contructie en het variabelen toekennen gelet, niet op de regex.

checken voor inhoud ben ik in m'n haast vergeten :o |:(

herkansing: (let niet op de leesbaarheid)
PHP:
1
<?function isDutchPostcode($postcode) {    return $postcode ? (preg_match(/^[1-9]d{3}s*[a-z]{2}$/i', $postcode) ? TRUE : FALSE) : FALSE;}?>

maare, zullen we benchmarks draaien? >:)

edit: grum's regex gebruikt :)

edit2: \'s on regex waren weg? blijkbaar snapt de ubb-parser van topix niet dat hij van \'s in stukken php af moet blijven...

  • ACM
  • Registratie: Januari 2000
  • Niet online

ACM

Software Architect

Werkt hier

Op dinsdag 16 april 2002 10:28 schreef GraasGast het volgende:
maare, zullen we benchmarks draaien? >:)
Doe je best, ik moet zo weg :)

Maar geef dan ook de resultaten met bijv 10% lege postcodes, 5% etc :)

  • Grum
  • Registratie: Juni 2001
  • Niet online
PHP 4.0.6
code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
Empty    : 100%
For    : 0.1991770267
Preg      : 1.8583589792 100
Empty + Preg: 1.3817759752  74 (+-25.6%)

Empty    : 75%
For    : 0.1991119385
Preg      : 1.9315379858 100
Empty + Preg: 1.6747610569  86 (+-13.3%)

Empty    : 50%
For    : 0.1990599632
Preg      : 1.9982119799 100
Empty + Preg: 1.9763180017  98 (+-1.1%)

Empty    : 25%
For    : 0.1991980076
Preg      : 2.0583539009 100
Empty + Preg: 2.2585979700 109 (+ 9.7%)

Empty    : 0%
For    : 0.1991140842
Preg      : 2.1263698339 100
Empty + Preg: 2.5561429262 120 (+20.2%)

4.1.2
code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
Empty    : 100%
For    : 0.2101559639
Preg      : 1.9603869915 100
Empty + Preg: 1.4277180433  72 (+-27.2%)

Empty    : 75%
For    : 0.2101949453
Preg      : 2.0178060532 100
Empty + Preg: 1.7290951014  85 (+-14.3%)

Empty    : 50%
For    : 0.2099250555
Preg      : 2.0822509527 100
Empty + Preg: 2.0443860292  98 (+-1.8%)

Empty    : 25%
For    : 0.2098909616
Preg      : 2.1599531174 100
Empty + Preg: 2.3615670204 109 (+ 9.3%)

Empty    : 0%
For    : 0.2103360891
Preg      : 2.1191719770 100
Empty + Preg: 2.5957869291 122 (+22.5%)

stukken gebenchte code:
code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
ob_start();
$theStartTime = microtime();
 
function isDutchPostcode($postcode)
{
   $isDutchPostcode = FALSE;
   if ($postcode != '')
   {
    if (preg_match('/^[0-9]{4} ?[a-zA-Z]{2}$/',$postcode))
       $isDutchPostcode = TRUE;
   }
   return $isDutchPostcode;
}
 
for ($i = 0; $i < $theRuns; $i++)
{
   if($i % 100 < $perc_empty)
    $postcode = '';
   elseif($i % 2)
    $postcode = '1324 as';
   else
    $postcode = 'asdsad 123';
 
   print isDutchPostCode($postcode)? 'Yeah': 'Nah';
}
 
$theEndTime = microtime();
ob_end_clean();

en
code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
ob_start();
$theStartTime = microtime();
 
function isDutchPostcodeAgain($postcode)
{
   $isDutchPostcode = FALSE;
   if (preg_match('/^[1-9]\d{3}\s*[A-Z]{2}$/i',$postcode))
    $isDutchPostcode = TRUE;
 
   return $isDutchPostcode;
}
 
for ($i = 0; $i < $theRuns; $i++)
{
   if($i % 100 < $perc_empty)
    $postcode = '';
   elseif($i % 2)
    $postcode = '1324 as';
   else
    $postcode = 'asdsad 123';
 
   print isDutchPostCodeAgain($postcode)? 'Yeah': 'Nah';
}
 
$theEndTime = microtime();
ob_end_clean();

Note: de pregs verschillen iets maar het verschil is 1% in het voordeel van de laatste (zowel onder php4.1 als 4.0)

  • GraasGast
  • Registratie: Oktober 2000
  • Laatst online: 03-09 17:11

GraasGast

Analogue Heaven

kan je deze ook ff benchen? :)
PHP:
1
<?function isDutchPostcode($postcode) {    return $postcode ? (preg_match(/^[1-9]d{3}s*[a-z]{2}$/i', $postcode) ? TRUE : FALSE) : FALSE;}?>

let op: voor de d en de s in de regex moet je ff een \ zetten!

Verwijderd

Wat is er mis met deze?
code:
1
2
3
function isDutchPostcode($postcode) {
    return preg_match('/^\d{4} ?[a-z]{2}$/i', $postcode);
}

  • Grum
  • Registratie: Juni 2001
  • Niet online
GraasGast: kan je deze ook ff benchen? :)

Nah .. ik moet eerst ff een goede bench set maken .. dit kost me teveel tijd zo :D

Tijd die ik niet echt heb ;) .. - werk - ! :o

  • Bosmonster
  • Registratie: Juni 2001
  • Laatst online: 29-08 19:47

Bosmonster

*zucht*

Op dinsdag 16 april 2002 11:14 schreef Arien het volgende:
Wat is er mis met deze?
code:
1
2
3
function isDutchPostcode($postcode) {
    return preg_match('/^\d{4} ?[a-z]{2}$/i', $postcode);
}
Postcodes mogen niet beginnen met een 0

  • D2k
  • Registratie: Januari 2001
  • Laatst online: 31-08 10:19

D2k

Op dinsdag 16 april 2002 11:19 schreef Bosmonster het volgende:
Postcodes mogen niet beginnen met een 0
tsk Arien ben je nu al zolang weg dat je dat niet meer weet ;)
< inside joke>

Doet iets met Cloud (MS/IBM)


  • GraasGast
  • Registratie: Oktober 2000
  • Laatst online: 03-09 17:11

GraasGast

Analogue Heaven

* GraasGast slaps himself with a very large trout |:(

update:
code:
1
2
3
function isDutchPostcode($postcode) {
    return $postcode ? preg_match(/^[1-9]\d{3}\s*[a-z]{2}$/i', $postcode) : FALSE;
}

cleaner kan het niet lijkt me :P

leesbaarder wel :)

Verwijderd

D2k: Tsk Arien ben je nu al zolang weg dat je dat niet meer weet ;)
:D

Ik had hem alleen gecopy-paste. Even goed dan:
code:
1
2
3
function isDutchPostcode($postcode) {
    return preg_match('/^[1-9]\d{3} ?[a-z]{2}$/i', $postcode);
}

Het ging me om de functie als geheel (itt de overbodige ... ? true : false). ;)

  • GraasGast
  • Registratie: Oktober 2000
  • Laatst online: 03-09 17:11

GraasGast

Analogue Heaven

Op dinsdag 16 april 2002 12:14 schreef Arien het volgende:

[..]

Het ging me om de functie als geheel (itt de overbodige ... ? true : false). ;)
is al gefixt, zie 2 posts omhoog :)

  • Grum
  • Registratie: Juni 2001
  • Niet online
code:
1
return $postcode ? preg_match(/^[1-9]\d{3}\s*[a-z]{2}$/i', $postcode) : FALSE;

sorry maar dit raakt kant nog wal

Verwijderd

Op dinsdag 16 april 2002 09:21 schreef drm het volgende:
't mooiste is natuurlijk als je een klasse hebt die generiek de formuliertjes genereert en ook automatisch controleert...
En hem vervolgens hier post :)

  • Janoz
  • Registratie: Oktober 2000
  • Laatst online: 09-09 20:58

Janoz

Moderator Devschuur®

!litemod

Ik mis nog een functie voor gebruikersnaam. Dat deze alleen uit letters + cijfers + enkele van te voren aangegeven karakters als bv _ en - mag bestaan.

deze heb ik ergens gebruikt:
PHP:
1
<?preg_match("/^[-A-Za-z0-9_]{4,20}$/",$field);?>

In deze mogen alleen letters cijfers en _ worden gebruikt. minimaal 4, maximaal 20 karakters.

Ken Thompson's famous line from V6 UNIX is equaly applicable to this post:
'You are not expected to understand this'


Verwijderd

GraasGast: Is al gefixt, zie 2 posts omhoog :)
Nee, er zit nog een onnodige ... ? ... : ... in.

Als je dan toch wilt kijken of $postcode niet leeg is, etc kan je net zo goed zeggen:
code:
1
2
3
4
function isDutchPostcode($postcode) {
    return $postcode and
      preg_match('/^[1-9]\d{3} ?[a-z]{2}$/i', $postcode);
}

  • GraasGast
  • Registratie: Oktober 2000
  • Laatst online: 03-09 17:11

GraasGast

Analogue Heaven

Op dinsdag 16 april 2002 12:49 schreef Arien het volgende:

[..]

Nee, er zit nog een onnodige ... ? ... : ... in.

Als je dan toch wilt kijken of $postcode niet leeg is, etc kan je net zo goed zeggen:
code:
1
2
3
4
function isDutchPostcode($postcode) {
    return $postcode and
      preg_match('/^[1-9]\d{3} ?[a-z]{2}$/i', $postcode);
}
Ik probeer te voorkomen dat preg_match() word aangeroepen als dat niet nodig is:
Op dinsdag 16 april 2002 10:26 schreef ACM het volgende:

[..]

Het laden van die preg-engine is best wel veel werk :)
[..]

  • GraasGast
  • Registratie: Oktober 2000
  • Laatst online: 03-09 17:11

GraasGast

Analogue Heaven

Op dinsdag 16 april 2002 12:29 schreef Grum het volgende:
code:
1
return $postcode ? preg_match(/^[1-9]\d{3}\s*[a-z]{2}$/i', $postcode) : FALSE;

sorry maar dit raakt kant nog wal
waarom niet? het doet gewoon dit:
code:
1
2
3
4
5
if ($postcode) {
    return preg_match(/^[1-9]\d{3}\s*[a-z]{2}$/i', $postcode)
} else {
    return FALSE;
}

  • ACM
  • Registratie: Januari 2000
  • Niet online

ACM

Software Architect

Werkt hier

Op dinsdag 16 april 2002 13:15 schreef GraasGast het volgende:
Ik probeer te voorkomen dat preg_match() word aangeroepen als dat niet nodig is:
Als het goed is gebeurd dat ook met die 'and' / '&&' (php doet toch aan lazy evaluation?)

  • GraasGast
  • Registratie: Oktober 2000
  • Laatst online: 03-09 17:11

GraasGast

Analogue Heaven

Op dinsdag 16 april 2002 13:20 schreef ACM het volgende:

[..]

Als het goed is gebeurd dat ook met die 'and' / '&&' (php doet toch aan lazy evaluation?)
hmm...als dat zo is dan ben ik inderdaad slap aan het lullen :)

  • ACM
  • Registratie: Januari 2000
  • Niet online

ACM

Software Architect

Werkt hier

Op dinsdag 16 april 2002 13:22 schreef GraasGast het volgende:
hmm...als dat zo is dan ben ik inderdaad slap aan het lullen :)
* ACM heeft het even snel getest :)
code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
acm@vulcanus:~ 2$ php
<?
$x = (false && print('hey'));
echo "\n$x\n";
?>
X-Powered-By: PHP/4.1.2
Content-type: text/html



acm@vulcanus:~ 3$ php
<?
$x = (true && print('hey'));
echo "\n$x\n";
?>
X-Powered-By: PHP/4.1.2
Content-type: text/html

hey
1
acm@vulcanus:~ 4$

  • Grum
  • Registratie: Juni 2001
  • Niet online
Maar zoals me benchmark al liet zien is in een reele situatie ZONDER 'pre-check' sneller :D

  • Stewie!
  • Registratie: September 2001
  • Laatst online: 09:57

Stewie!

Keen must die!

Op dinsdag 16 april 2002 09:16 schreef LOST_GUNT het volgende:
offtopic:
isLekkerWijf()
Verdent extra aandacht :P


:D
zo'n functie zou ik ook wel willen gebruiken :o :*


Strava: https://www.strava.com/athletes/149347154


  • brammetje
  • Registratie: Oktober 2000
  • Laatst online: 12-01-2025
Op dinsdag 16 april 2002 11:10 schreef GraasGast het volgende:
-knip-
waarom doe je in hemelsnaam dit: ? TRUE : FALSE ??

Als je korte code wilt dan haal je dat toch ook gewoon weg?


edit: tweede pagina niet gelezen :X
Pagina: 1