const regex = /^((ftp|http|https)://)?(www.)?(?!.*(ftp|http|https|www.))[a-zA-Z0-9_-]+(.[a-zA-Z]+)+((/)[w#]+)*(/w+?[a-zA-Z0-9_]+=w+(&[a-zA-Z0-9_]+=w+)*)?$/gm;
const str = `http://www.sample.com
https://www.sample.com
http://www.sample.com/xyz
www.sample.com
www.sample.com/xyz/#/xyz
sample.com
www.sample.com
mofiz.com
kolim.com
www.murikhao.www.sample.com
http://murihao.www.sample.com
http://www.sample.com/xyz?abc=dkd&p=q&c=2
www.sample.gov.bd
www.sample.com.en
www.sample.vu
`;
let m;
while ((m = regex.exec(str)) !== null) {
if (m.index === regex.lastIndex) {
regex.lastIndex++;
}
console.log("matched :"+m[0]);
}
/^(?:(?:https?|ftp)://)(?:S+(?::S*)?@)?(?:(?!(?:10|127)(?:.d{1,3}){3})(?!(?:169.254|192.168)(?:.d{1,3}){2})(?!172.(?:1[6-9]|2d|3[0-1])(?:.d{1,3}){2})(?:[1-9]d?|1dd|2[01]d|22[0-3])(?:.(?:1?d{1,2}|2[0-4]d|25[0-5])){2}(?:.(?:[1-9]d?|1dd|2[0-4]d|25[0-4]))|(?:(?:[a-zu00a1-uffff0-9]-*)*[a-zu00a1-uffff0-9]+)(?:.(?:[a-zu00a1-uffff0-9]-*)*[a-zu00a1-uffff0-9]+)*(?:.(?:[a-zu00a1-uffff]{2,})).?)(?::d{2,5})?(?:[/?#]S*)?$/i
/((?:(?:http?|ftp)[s]*://)?[a-z0-9-%/&=?.]+.[a-z]{2,4}/?([^s<>#%",{}|^[]`]+)?)/gi
$regex = "((https?|ftp)://)?";
$regex .= "([a-z0-9+!*(),;?&=$_.-]+(:[a-z0-9+!*(),;?&=$_.-]+)?@)?";
$regex .= "([a-z0-9-.]*).([a-z]{2,3})";
$regex .= "(:[0-9]{2,5})?";
$regex .= "(/([a-z0-9+$_-].?)+)*/?";
$regex .= "(?[a-z+&$_.-][a-z0-9;:@&%=+/$_.-]*)?";
$regex .= "(#[a-z_.-][a-z0-9+$_.-]*)?";
$url = 'https://mydomain.com/';
if (preg_match("/^$regex$/i", $url)) {
echo('Enter URL is a valid URL');
}