My Regex is also sub par:
<?php
$urls = array (
'a',
'http://a',
'www.a',
'test.com',
'http://test.com',
'http://www.test.com',
'http://www.test.com?searchblah&blajjj',
'http://www.te$%^$.com',
'http://test.1223',
'http://test.co',
'http://test.co.uk',
'http://sub.test.com',
);
foreach ( $urls as $url ) {
echo $url.' --- '. ( preg_match ( '#^http(s)?://(www\.)?([a-z0-9_\-\.]+)\.([a-z\.]){2,5}([/\?&\#\w=]+)?$#', $url ) ? 'Valid' : 'Not Valid' ).
'<br />';
}
a --- Not Valid
http://a --- Not Valid
www.a --- Not Valid
test.com --- Not Valid
http://test.com --- Valid
http://www.test.com --- Valid
http://www.test.com?searchblah&blajjj --- Valid
http://www.te$%^$.com --- Not Valid
http://test.1223 --- Not Valid
http://test.co --- Valid
http://test.co.uk --- Valid
http://sub.test.com --- Valid
Edit: Edited expression to match '=' also ( for the likes of ?blah=123 )