php - check if a string is a URL -
this question has answer here:
- best way check if url valid 8 answers
i've seen many questions wasn't able understand how works want more simple case.
if have text, whatever is, i'd check if url or not.
$text = "something.com"; //this url if (!isurl($text)){ echo "no not url"; exit; // die }else{ echo "yes url"; // else codes goes } function isurl($url){ // ??? }
is there other way rather checking javascript in case js blocked?
http://www.php.net/manual/en/function.preg-match.php#93824
<?php $regex = "((https?|ftp)\:\/\/)?"; // scheme $regex .= "([a-z0-9+!*(),;?&=\$_.-]+(\:[a-z0-9+!*(),;?&=\$_.-]+)?@)?"; // user , pass $regex .= "([a-z0-9-.]*)\.([a-z]{2,3})"; // host or ip $regex .= "(\:[0-9]{2,5})?"; // port $regex .= "(\/([a-z0-9+\$_-]\.?)+)*\/?"; // path $regex .= "(\?[a-z+&\$_.-][a-z0-9;:@&%=+\/\$_.-]*)?"; // query $regex .= "(#[a-z_.-][a-z0-9+\$_.-]*)?"; // anchor if(preg_match("/^$regex$/i", $url)) // `i` flag case-insensitive { return true; } ?>
but example url on simplified, (\w+)\.(\w+)
match it. else mentioned filter_var
filter_var($url, filter_validate_url)
doesn't seem non-ascii characters so, beware...
Comments
Post a Comment