Domain Cleanup Regular Expressions

Wednesday, November 4, 2009

Domain Cleanup Regular Expressions

If you have to prompt the user to enter a domain could use these php regular expressions to *clean up* the domain name.

<?php

// devcha.com
$domain = empty($_REQUEST['domain']) ? '' : trim($_REQUEST['domain']);

$domain = preg_replace('#^(ht|f)tps?:/*#si', '', $domain); // http OR https OR ftp OR ftps
$domain = preg_replace('#^w+\.#si', '', $domain); // www.domain...
$domain = preg_replace('#/.*#si', '', $domain); // e.g. domain.com/afsaksfasfj

?>

 Digg  Del.icio.us  Reddit  SlashDot

0 comments: