1<?php
2 $regex = "((https?|ftp)\:\/\/)?"; // SCHEME
3 $regex .= "([a-z0-9+!*(),;?&=\$_.-]+(\:[a-z0-9+!*(),;?&=\$_.-]+)?@)?"; // User and Pass
4 $regex .= "([a-z0-9-.]*)\.([a-z]{2,3})"; // Host or IP
5 $regex .= "(\:[0-9]{2,5})?"; // Port
6 $regex .= "(\/([a-z0-9+\$_-]\.?)+)*\/?"; // Path
7 $regex .= "(\?[a-z+&\$_.-][a-z0-9;:@&%=+\/\$_.-]*)?"; // GET Query
8 $regex .= "(#[a-z_.-][a-z0-9+\$_.-]*)?"; // Anchor
9
10 if(preg_match("/^$regex$/i", $url)) // `i` flag for case-insensitive
11 {
12 return true;
13 }
14?>