Advertisement
dimipan80

Email Censorship

Apr 28th, 2015
264
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.90 KB | None | 0 0
  1. <?php
  2. $text = $_GET['text'];
  3. $blacklist = preg_split('/[*\s]+/', $_GET['blacklist'], -1, PREG_SPLIT_NO_EMPTY);
  4.  
  5. $emailPattern = "/\b([\w+-]+)@([A-Za-z0-9-]+)\.([A-Za-z0-9.-]+)\b/";
  6. if (preg_match_all($emailPattern, $text, $matches)) {
  7.     $fullEmails = $matches[0];
  8.     $domainParts = $matches[3];
  9.     foreach ($fullEmails as $key => $email) {
  10.         $domain = $domainParts[$key];
  11.         $isBlackListed = false;
  12.         if (in_array($email, $blacklist)) {
  13.             $isBlackListed = true;
  14.         } else {
  15.             foreach ($blacklist as $blacklisted) {
  16.                 if ($blacklisted[0] == '.' && (strpos($blacklisted, $domain) === 1)
  17.                 ) {
  18.                     $isBlackListed = true;
  19.                     break;
  20.                 }
  21.             }
  22.         }
  23.  
  24.         if ($isBlackListed) {
  25.             $replacement = str_repeat('*', strlen($email));
  26.         } else {
  27.             $replacement = '<a href="mailto:' . $email . '">' . $email . '</a>';
  28.         }
  29.  
  30.         $text = str_replace($email, $replacement, $text);
  31.     }
  32. }
  33. ?>
  34.  
  35. <p><?= $text ?></p>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement