Forum Moderators: coopster
preg_match('/\b(\d+)\s+(\w+)\s+(\w+)\b/', trim($input_value))
But to do a complete test you'll need a lot more to take into account street names with multiple words (123 Important Person Pkwy), different street types, street direction (123 1st Ave West), street number suffixes (123A Main St), and apt numbers, something like (without apt numbers):
preg_match(
'{\b\d+([A-Z]¦\d/\d)?\s+(\w+\s+)+\w+\.(\s+([nesw]¦[ns][ew]))?\b}i',
trim($input_value))
breaking it down:
\b is word boundry
\d+([A-Z]¦\d/\d)? matches street number with an optional letter suffix or 1/2,1/4,etc suffix
(\w+\s+)+ matches one or more words for the street name
\w+\. matches the street type
(\s+([nesw]¦[ns][ew]))* matches an optional street direction (n,s,e,w,ne,se,nw,sw)
This is from Canadian addresses, other countries might have different conventions.
preg_match("{http://www\.[a-z0-9-_]+(\.[a-z]+){1,2}/[a-z0-9/-_]}i", trim($_POST['url']))
this will match http://www.example.com/user-24 and also http://www.example.co.uk/user-24 etc, add a ^ and $ to the beginning and end of the pattern to validate the entire string.
You might also want to look into parse_url: [php.net...] e.g.
$url_parts = @parse_url(trim($_POST['url']));
if($url_parts === false)
echo 'bad url';
else if($url_parts['scheme']!= 'http')
echo 'only http allowed';
...