RAYCHEV

SampleExamV1-2Email_Censorship

Jan 20th, 2017
404
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.45 KB | None | 0 0
  1. <?php
  2. function endsWith($email, $blackListItem)
  3. {
  4.     $length = strlen($blackListItem);
  5.     if ($length == 0) {
  6.         return true;
  7.     }
  8.  
  9.     return (substr($email, -$length) === $blackListItem);
  10. }
  11.  
  12. //$text = $_GET['text'];
  13. //$blacklist = $_GET['blacklist'];
  14.  
  15. $text  = "Hi, I'm an air-conditioner technician, so if you need any assistance you can contact me
  16. at
  17. . Secondary email:
  18. or
  19. at
  20. OR
  21. ";
  22. $blacklist = "*.bg
  23.            [email protected]
  24.            *.com";
  25.  
  26. $blackListItems = preg_split("/[\*\s,]+/", $blacklist, -1, PREG_SPLIT_NO_EMPTY);
  27.  
  28.  
  29. preg_match_all("/[0-9A-Za-z+_-]+\@[0-9A-Za-z-]+\.[0-9A-Za-z-\.]+/" , $text, $matches);
  30.  
  31. foreach ($matches[0] as $matchArr) {
  32.  
  33.     foreach ($blackListItems as $blackListItem) {
  34.  
  35.         if (endsWith($matchArr, $blackListItem)) {
  36.  
  37.             $matchReplKeys[] = $matchArr;
  38.         }
  39.     }
  40. }
  41. foreach ($matchReplKeys as $matchReplKey) {
  42.     $asterix='';
  43.     for ($i=0; $i<strlen($matchReplKey); $i++ ) {
  44.         $asterix .= "*";
  45.     }
  46.  
  47.     $text = str_replace($matchReplKey, $asterix, $text);
  48. }
  49.  
  50. preg_match_all("/[0-9A-Za-z+_-]+\@[0-9A-Za-z-]+\.[0-9A-Za-z-\.]+/" , $text, $validEmails);
  51.  
  52. foreach ($validEmails[0] as $validEmail) {
  53.  
  54.     $mailToTag = '<a href="mailto:'.$validEmail.'">'.$validEmail.'</a>';
  55.     $text = str_replace($validEmail, $mailToTag, $text);
  56.  
  57. }
  58. echo "<p>".$text."</p>";
Advertisement
Add Comment
Please, Sign In to add comment