For a form like this
<form method='post' action=''>
<input type='url' name='urlink'>
<button type='submit' name='submit'>Submit</button>
</form>
How to validate any entered URL
and be sure that it is safe to be used in the element
<embed>, <frame>, <iframe>, <source>, <img>
What i do is the following steps
<?php
$url = $_POST['urlink'];
$url = filter_var($url, FILTER_SANITIZE_URL);
$url = preg_replace('#((https?|ftp)://(\S*?\.\S*?))([\s)\[\]{},;"\':<]|\.\s|$)#i',"'<a href=\"$1\" target=\"_blank\">$3</a>$4'", $url);
if(filter_var($url, FILTER_VALIDATE_URL) && preg_match('/(?i)\b((?:https?://|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}/)(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:'\".,<>?«»“”‘’]))/", $url)){
echo "SAFE URL";
} else {
echo "UNSAFE URL";
}
?>
Is that enough for it to be sure that the URL
is safe?