Guest User

Untitled

a guest
Feb 14th, 2019
164
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.58 KB | None | 0 0
  1. <?php
  2. //use a site with lots of email address on.
  3. $string = file_get_contents("https://stackoverflow.com/questions/3901070/in-php-how-do-i-extract-multiple-e-mail-addresses-from-a-block-of-text-and-put");
  4.  
  5. //initialise an empty array.
  6. $matches = array();
  7.  
  8. //regular expression that matches most email addresses, courtesy of @Eric-Karl.
  9. $pattern = '/[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+.[A-Za-z]{2,4}/';
  10.  
  11. //perform global regular expression match, ie search the entire web page for a particular thing, and store it in the previously initialised array.
  12. preg_match_all($pattern, $string, $matches);
  13.  
  14. //output array of values; remove duplicate email addresses, but maintain incremental key count.
  15. var_dump(array_values(array_unique($matches[0])));
  16.  
  17. //store above in array for upcoming bit.
  18. $neaterArray = array_values(array_unique($matches[0]));
  19.  
  20. //count the amount of variables in the array.
  21. $count = count($neaterArray);
  22.  
  23. //implode array values to string.
  24. $emailsAsString = implode(", ", $neaterArray);
  25.  
  26. //present the string, prepended with the count.
  27. echo "<h3>$count email addresses in total:</h3> $emailsAsString";
  28.  
  29. ?>
  30.  
  31. array (size=19)
  32. 0 => string 'apple-touch-icon@2.png' (length=22)
  33. 1 => string 'example@slu.edu' (length=15)
  34. 2 => string 'a+b@google.com.sg' (length=17)
  35. 3 => string 'test1+2@gmail.com' (length=17)
  36. 4 => string 'test-2@yahoo.co.jp' (length=18)
  37. 5 => string 'test@test.com' (length=13)
  38. 6 => string 'test@test.co.uk' (length=15)
  39. 7 => string 'test@google.com.sg' (length=18)
  40. 8 => string 'email@domain.info' (length=17)
  41. 9 => string 'email@domain.inf' (length=16)
  42. 10 => string 'first.lastname@domain.be' (length=24)
  43. 11 => string 'lastname@domain.be' (length=18)
  44. 12 => string 'HIDDENFORLOGICALREASONS@cameranh.rs.gov.br' (length=42)
  45. 13 => string 'HIDDENFORLOGICALREASONS@cameranh.rs.go' (length=38)
  46. 14 => string 'myemail@office21.company.com' (length=28)
  47. 15 => string 'mymail@yahoo.com' (length=16)
  48. 16 => string 'my-e.mail@yahoo.com' (length=19)
  49. 17 => string 'joe@mysite.com' (length=14)
  50. 18 => string 'name@example.com.sv' (length=19)
  51. 19 email addresses in total:
  52.  
  53. apple-touch-icon@2.png, example@slu.edu, a+b@google.com.sg, test1+2@gmail.com, test-2@yahoo.co.jp, test@test.com, test@test.co.uk, test@google.com.sg, email@domain.info, email@domain.inf, first.lastname@domain.be, lastname@domain.be, HIDDENFORLOGICALREASONS@cameranh.rs.gov.br, HIDDENFORLOGICALREASONS@cameranh.rs.go, myemail@office21.company.com, mymail@yahoo.com, my-e.mail@yahoo.com, joe@mysite.com, name@example.com.sv
  54.  
  55. function file_get_contents_curl($url) {
  56. $ch = curl_init();
  57.  
  58. curl_setopt($ch, CURLOPT_AUTOREFERER, TRUE);
  59. curl_setopt($ch, CURLOPT_HEADER, 0);
  60. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  61. curl_setopt($ch, CURLOPT_URL, $url);
  62. curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
  63.  
  64. $data = curl_exec($ch);
  65. curl_close($ch);
  66.  
  67. return $data;
  68. }
  69.  
  70. /[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+.[A-Za-z]{2,4}/
  71.  
  72. /[a-z0-9._%+-]+@[a-z0-9.-]+.[a-z]{2,4}/i
  73.  
  74. var_dump(array_values(array_unique($matches[0])));
  75.  
  76. $neaterArray = array_values(array_unique($matches[0]));
  77.  
  78. $neaterArray = (array_values(array_unique($matches[0])));
  79. var_dump($neaterArray);
  80.  
  81. $pattern = '/[w.%+-]+@(?:[a-zd-]+.)+[a-z]{2,4}/iu';
  82. if (!preg_match_all($pattern, $contents, $emails)) {
  83. echo '<h3>No emails found</h3>';
  84. } else {
  85. $unique_emails = array_flip(array_flip($emails[0]));
  86. $count = sizeof($unique_emails);
  87. $email_string = implode(', ', $unique_emails);
  88. echo "<h3>$count email addresses in total:</h3> $email_string";
  89. }
Add Comment
Please, Sign In to add comment