|
|
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: 412 Location: A'dam/Diemen, The Netherlands
|
Posted: Tue Sep 23, 2008 12:09 pm Post subject: preg_match, regexp, regular expressions |
|
|
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:
- http://php-regex.blogspot.com/ ( PHP Regular Expression Tutorial )
- http://www.regular-expressions.info/examples.html ( Sample Regular Expressions )
- http://www.regular-expressions.info/reference.html ( Regular Expression Basic Syntax Reference )
- http://www.tbforum.nl/artikel/89939.html (Dutch)
- 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 |
|
 |
Google adsense Advertisement
|
Posted: Tue Sep 23, 2008 12:09 pm Post subject: preg_match, regexp, regular expressions |
|
|
Advertisement
|
|
Back to top |
|
 |
GravityForms Advertisement
|
Posted: Tue Sep 23, 2008 12:09 pm Post subject: preg_match, regexp, regular expressions |
|
|
Advertisement
 |
|
Back to top |
|
 |
|
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
|
|
|
|