-2

I have the code below in the submitted form section of a php file. It is meant to catch any emails that contain a url and reject them.

    if (preg_match("/(\b(((https?|ftp|file|):\/\/)|www[.])[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/i", $msg)) {
      return false;
    }
    return true;   

But I received an email with dozens of lines like this:

[url=http://example.рф]шкафы купе[/url]

I tried sending a message with one of the lines in the original email and the code blocked it. Why didn't it stop this spammer?

user3052443
  • 758
  • 1
  • 7
  • 22
  • 2
    Because `рф` aren't included in the character class `[-A-Z0-9+&@#\/%=~_|]` – Toto Dec 26 '18 at 14:43
  • 1
    Use PHP filers. Why would you even bother writing your own custom validator? [PHP Validate filters](http://php.net/manual/en/filter.filters.validate.php) – Dharman Dec 26 '18 at 14:44
  • Have you tested your regex? It does not seem to match any urls... – jeroen Dec 26 '18 at 14:45
  • Possible duplicate of [How to check if a string contains a email?](https://stackoverflow.com/questions/38798199/how-to-check-if-a-string-contains-a-email) – Dharman Dec 26 '18 at 14:47
  • Possible duplicate of [How to validate an Email in PHP?](https://stackoverflow.com/questions/5855811/how-to-validate-an-email-in-php) – miken32 Dec 31 '18 at 18:11
  • To clarify, the question had to do with text entered in an emails body that might contain a url, not with the actual emails. So the possible duplicates are not correct although I could see that might question may have caused confusion. – user3052443 Jan 09 '19 at 19:11

1 Answers1

0

Based on the replies, which I deeply appreciate, I changed the code to the following. All I really want is to block url's. It doesn't matter what the path and parameters are so I think this catches all possibilities. This is used instead of php filters because the filters page says it can't catch URN's.

    if (preg_match("/(\b(((https?|ftp|file|[-A-Z0-9]|):\/\/)|www[.]))/i", $msg)) {
user3052443
  • 758
  • 1
  • 7
  • 22