PHP: Function to Strip URLS from Text
You are here
Prevent users from posting unwanted URL, and protect yourself from malicious posts or in appropriate urls being posted in your content.
function removeURLs($haystack) {
// unwanted tags separated by vertical bar
$strip_tags = "a|script|p|font|style";
$haystack = preg_replace("#<\s*\/?(".$strip_tags.")\s*[^>]*?>#im", '', $haystack);
// http(s)://
$haystack = preg_replace('|https?://www\.[-a-zA-Z\.0-9]+|i', '', $haystack);
// only www.
$haystack = preg_replace('|www\.[-a-zA-Z\.0-9]+|i', '', $haystack);
// Back Up
$haystack = preg_replace('/\b(https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|$!:,.;]*[A-Z0-9+&@#\/%=~_|$]/i', '', $haystack);
$haystack = str_replace('<a href="', "", $haystack);
$haystack = str_replace('">', "", $haystack);
$haystack = str_replace('</a>', "", $haystack);
return $haystack;
}