Terug naar de voorpaginaTBForum nu ook op je mobiel!
Klik hier voor meer info en gratis link...

Home Nieuw Vraag & Aanbod Forums Artikelen Bedrijvengids Zoeken

   Erwin van den Boom, TargetMedia
21 okt 2008 19:32 
For those who don't want to read themselves into the regular expression syntax I have made a brief compilation of useful snippets in PHP:

Strip unwanted characters

Remove all special characters, except alphabetical (case-insensitive):

$output = preg_replace("/[^a-z]/i", "", $input);

Remove all except digits and alphabetical characters (case-insensitive):

$output = preg_replace("/[^a-z\d]/i", "", $input);

Remove all except digits, alphabetical and spaces (case-insensitive):

$output = preg_replace("/[^a-z \d]/i", "", $input);

Remove excess spaces:

$output = preg_replace('/\s\s+/', ' ', $input);


Make clickable

Make an URL clickable:

$output = 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);
$output = 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);

Make e-mailaddresses clickable:

$output = preg_replace("#([\n ])([a-z0-9\-_.]+?)@([\w\-]+\.([\w\-\.]+\.)*[\w]+)#i", "\\1<a href=\"mailto:\\2@\\3\" class=orange>\\2@\\3</a>", $input);


Validation functions

To test if an e-mailaddress is valid:

$test = preg_match("/^[a-z0-9_\.-]+@([a-z0-9]+([\-]+[a-z0-9]+)*\.)+[a-z]{2,7}$/i", $input);


To test if an URL is valid:

$test = preg_match('/(https?:\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/i', $input);


Feel free to leave your own useful reg-ex patterns in the comments!

   Matthijs Huisman, Huisman Drukwerk BV
21 okt 2008 20:37 
$output = preg_replace("#([\n ])([a-z0-9\-_.]+?)@([\w\-]+\.([\w\-\.]+\.)*[\w]+)#i", "\\1<a href=\"mailto:\\2@\\3\" class=orange>\\2@\\3</a>", $input);

"class=orange"


Direct copy from the Tbforum source?

   Richard O., Particulier
21 okt 2008 21:06 
In the e-mail addresses functions
([a-z0-9\-_.]+?)

[a-z0-9_\.-]

I would recommend to prefix both . and - with a backslash.
In the first example, . matches any char, instead of literal dot.

The same goes for the dot in the URL check function
[-A-Z0-9+&@#\/%?=~_|!:,.;]

should be
[-A-Z0-9+&@#\/%?=~_|!:,\.;]

   Pepijn P., Particulier
21 okt 2008 21:20 
I find it an useful article, but you forgot to translate the title

EDIT: It's translated, that was fast

     Aangepast op 21-10-2008 21:35 door Pepijn P.
   Ramon Fincken, CreativePulses.nl / WebsiteFreel
22 okt 2008 18:33 
Linked @ (Log in om link te zien!)
Including PHP validation functions ( dutch postcode etc.. ) plus an overview of special character-sets( [:digit:] Only the digits 0 to 9 , etc.. )


Also interesting:
# (Log in om link te zien!) ( PHP Regular Expression Tutorial )

# (Log in om link te zien!) ( Sample Regular Expressions )

# (Log in om link te zien!) ( Regular Expression Basic Syntax Reference )




 
© Copyright TargetMedia 2001-2012 | Mobile | Premium SMS | Micropayments | Muziek downloaden | Ringtones Bekijk bezoekers statistieken RSS feed