📜 ⬆️ ⬇️

Search for hidden contacts by regular expressions

There are a lot of materials on regulars, a lot of ready-made templates that you can just take and use in validators, searches, etc. Usually these are regular recurring lists to validate, for example, the entered email address. But I recently had the task of finding “incorrect” data (when users know that they cannot write their contacts, but write in perverted ways), specifically emails and telephones.

The task was this: " Do not give users to publish their phone numbers and email addresses on the site ."

I, unfortunately, could not find a ready-made solution on the Internet and therefore I want to share a couple of regular expressions that I wrote for these purposes.
')


Phone:
^\s[-_\[\]0-9 ()]{10,20}^
Email:
^[-.\w]+(?:[-_( \[])*(?:@|at)(?:[-_) \]])*(?:[-\w])+(?:[-_ \[(]*)(?:[.]|dot)(?:[-_ \])]*)(?:[\w.]*)^

These regulars covered all my needs for finding hidden contact information on the site I needed.

Here is a good example of what kind of address I’ve met:
Email example

I’ll clarify that the site is not Russian, so the expressions are focused on the English-speaking contingent of users.

PS
for PHP lovers, you can make helpers of the following form:

 if (!function_exists('getPhonePattern')) { function getPhonePattern() { return '^\s[-_\[\]0-9 ()]{10,20}^'; } } if (!function_exists('getEmailPattern')) { function getEmailPattern() { return '^[-.\w]+(?:[-_( \[])*(?:@|at)(?:[-_) \]])*(?:[-\w])+(?:[-_ \[(]*)(?:[.]|dot)(?:[-_ \])]*)(?:[\w.]*)^'; } } if (!function_exists('removeContacts')) { function removeContacts($data) { $data = preg_replace(getEmailPattern(), ' *** hidden email *** ', $data); $data = preg_replace(getPhonePattern(), ' *** hidden phone *** ', $data); return $data; } } 
if (!function_exists('getPhonePattern')) { function getPhonePattern() { return '^\s[-_\[\]0-9 ()]{10,20}^'; } } if (!function_exists('getEmailPattern')) { function getEmailPattern() { return '^[-.\w]+(?:[-_( \[])*(?:@|at)(?:[-_) \]])*(?:[-\w])+(?:[-_ \[(]*)(?:[.]|dot)(?:[-_ \])]*)(?:[\w.]*)^'; } } if (!function_exists('removeContacts')) { function removeContacts($data) { $data = preg_replace(getEmailPattern(), ' *** hidden email *** ', $data); $data = preg_replace(getPhonePattern(), ' *** hidden phone *** ', $data); return $data; } }


Do not forget that you can complement these regulars.

Source: https://habr.com/ru/post/81220/


All Articles