preg_match, regexp, regular expressions

By.

min read

My profile

Share this:

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:

[list=a:4a0be7b5ac][*:4a0be7b5ac][url]http://php-regex.blogspot.com/[/url] ( PHP Regular Expression Tutorial )

[*:4a0be7b5ac][url]http://www.regular-expressions.info/examples.html[/url] ( Sample Regular Expressions )

[*:4a0be7b5ac][url]http://www.regular-expressions.info/reference.html[/url] ( Regular Expression Basic Syntax Reference )

[*:4a0be7b5ac][url]http://www.tbforum.nl/artikel/89939.html[/url] (Dutch)
[*:4a0be7b5ac][url]http://www.tbforum.com/artikel/117842.html[/url] (English)[/list:o:4a0be7b5ac]

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

[code:1:4a0be7b5ac] 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(“#([trn ])([a-z0-9]+?){1}://([w-]+.([w-]+.)*[w]+(:[0-9]+)?(/[^ “nrt<]*)?)#i”, ‘1<a href=”2://3″ target=”_blank” rel=”nofollow”>3</a>’, $input);
$input = preg_replace(“#([trn ])(www).(([w-]+.)*[w]+(:[0-9]+)?(/[^ “nrt<]*)?)#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;
}[/code:1:4a0be7b5ac]

This list may come in handy too..

[code:1:4a0be7b5ac]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.[/code:1:4a0be7b5ac]

Share this:

Leave a Reply

Your email address will not be published. Required fields are marked *