Top Poster
#1 ramon fincken 400
#2 Site admin 3
#3 Ber|Art 2
#4 braddmark 1
#5 accentmedia 1
#6 Xarzu 1
#7 pEcosespedex 1
#8 khadish 1
#9 moxxx 1
#10 nistelrock 1
preg_match, regexp, regular expressions



Rating : 3.4 / 5

 
Post new topic   Reply to topic    Ramonfincken.com Forum Index -> PHP coding
Beta
Want to be notified by email when this topic gets a reply?  
View previous topic :: View next topic  
Author Message
ramon fincken
Site's programmer

Get a free globally recognized avatar
It's free!


Joined: 03 Aug 2007
Posts: 400
Location: A'dam/Diemen, The Netherlands

PostPosted: Tue Sep 23, 2008 12:09 pm    Post subject: preg_match, regexp, regular expressions Reply with quote

preg_match and equivalent expressions are great !
The only problem is that there are just a few good tutorials around ...

So these are the tutorials I always use:

  1. http://php-regex.blogspot.com/ ( PHP Regular Expression Tutorial )

  2. http://www.regular-expressions.info/examples.html ( Sample Regular Expressions )

  3. http://www.regular-expressions.info/reference.html ( Regular Expression Basic Syntax Reference )

  4. http://www.tbforum.nl/artikel/89939.html (Dutch)
  5. http://www.tbforum.com/artikel/117842.html (English)





Some examples from my validate class including Dutch validation of post/zipcode and postal number:

Code:
   function postcode($postcode)
   {
      if (preg_match('/^[0-9]{4} ?[A-Za-z]{2}$/', $postcode)) {
         return true;
      }
      return false;
   }

   function alphanum($az)
   {
      if (preg_match('/^([[:alnum:]])+$/', $az)) {
         return true;
      }
      return false;
   }
 function straat_en_huisnummer($straat) {
      if (preg_match('/^([[:alnum:]] ?\-?)+ ?[0-9\-]{1,6} ?[A-Za-z]{0,3}$/', $straat)) {
       return true;
     }
     return false;
 }
  function huisnummer($huisnummer) {
      if (preg_match('/^[0-9\-]{1,6} ?[A-Za-z]{0,3}$/', $huisnummer)) {
         return true;
     }
     return false;
 }

   /**
    * {a-z, space, .}
    */   
   function convert_azdots($input)
   {
      return preg_replace("/[^a-z .]/i", "", $input);
   }
   
   /**
    * {a-z, space, 0-9, ., -, +, (,)}
    */
   function convert_phone($phone)
   {
      return preg_replace("/[^a-z \d.\-+()]/i", "", $phone);
   }
   
   function convert_hrefs($input)
   {
      $input = preg_replace("#([\t\r\n ])([a-z0-9]+?){1}://([\w\-]+\.([\w\-]+\.)*[\w]+(:[0-9]+)?(/[^ \"\n\r\t<]*)?)#i", '\1<a href="\2://\3" target="_blank" rel="nofollow">\3</a>', $input);
      $input = preg_replace("#([\t\r\n ])(www)\.(([\w\-]+\.)*[\w]+(:[0-9]+)?(/[^ \"\n\r\t<]*)?)#i", '\1<a href="http://\2.\3" rel="nofollow" target="_blank">\2.\3</a>', $input);
      $input = preg_replace("#([\n ])([a-z0-9\-_.]+?)@([\w\-]+\.([\w\-\.]+\.)*[\w]+)#i", "\\1<a href=\"mailto:\\2@\\3\" class=orange>\\2@\\3</a>", $input);
      return $input;
   }



This list may come in handy too..

Code:
POSIX Character Class Definitions
Value
   
Meaning
[:digit:]  Only the digits 0 to 9
[:alnum:]   Any alphanumeric character 0 to 9 OR A to Z or a to z.
[:alpha:]    Any alpha character A to Z or a to z.
[:blank:]   Space and TAB characters only.
[:xdigit:]   Hexadecimal notation 0-9, A-F, a-f.
[:punct:]  Punctuation symbols . , " ' ? ! ; : # $ % & ( ) * + - / < > = @ [ ] \ ^ _ { } | ~
[:print:]  Any printable character.
[:space:]    Any whitespace characters (space, tab, NL, FF, VT, CR). Many system abbreviate as \s.
[:graph:]   Exclude whitespace (SPACE, TAB). Many system abbreviate as \W.
[:upper:]  Any alpha character A to Z.
[:lower:]    Any alpha character a to z.
[:cntrl:]    Control Characters NL CR LF TAB VT FF NUL SOH STX EXT EOT ENQ ACK SO SI DLE DC1 DC2 DC3 DC4 NAK SYN ETB CAN EM SUB ESC IS1 IS2 IS3 IS4 DEL.
Back to top
View user's profile Send private message Visit poster's website
Google adsense
Advertisement





PostPosted: Tue Sep 23, 2008 12:09 pm    Post subject: preg_match, regexp, regular expressions

Advertisement
Back to top
GravityForms
Advertisement





PostPosted: Tue Sep 23, 2008 12:09 pm    Post subject: preg_match, regexp, regular expressions

Advertisement
Gravity Forms Plugin for WordPress
Back to top
Post new topic   Reply to topic    Ramonfincken.com Forum Index -> PHP coding All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
General forums
The world of freelance
Programming
Other projects
Poll
I love gifts

Yes goodies and presents! [3]
No but I love polls ! [1]

Related google ads