RussellHarrower
RussellHarrower

Reputation: 6820

strip URLs but allow email addresses

Hi i am using the following function to remove urls but i found it also removes email addresses.

One thing I want is for users to be able to send there email address to another user but not send urls.

function cleaner($url) {
  $U = explode(' ',$url);
  $W =array();
  foreach ($U as $k => $u) {
    if (stristr($u,'http') || (count(explode('.',$u)) > 1)) {
      unset($U[$k]);
      return cleaner( implode(' ',$U));
    }
  }
  return implode(' ',$U);
}

$url = "Here is another funny site www.tinyurl.com/55555 and http://www.tinyurl.com/55555 and img.hostingsite.com/badpic.jpg";
echo "Cleaned: " . cleaner($url);

Upvotes: 1

Views: 204

Answers (1)

Nev Stokes
Nev Stokes

Reputation: 9819

I'd try using a proper regular expression to match URLs as opposed to something that just matches periods. Give this one a go: http://daringfireball.net/2010/07/improved_regex_for_matching_urls

Run it through preg_replace and you should achieve what you want.

Upvotes: 2

Related Questions