fork download
  1. <?php
  2.  
  3. $text = "Sample text for testing:
  4. abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ
  5. 0123456789 +-.,!@#$%^&*();\/|<>'
  6. 12345 -98.7 3.141 .6180 9,000 +42
  7. 555.123.4567 +1-(800)-555-2468
  8. foo@demo.net bar.ba@test.co.uk
  9. www.demo.com http://f...content-available-to-author-only...o.uk/
  10. http://r...content-available-to-author-only...r.com/foo.html?q=bar
  11. mail@mail.ru waka@wak.rc.uk
  12. user0+test+hello_world@100500.com
  13. user%office@company.com
  14. BIGBOSS@BIGCOMPANY.COM
  15. ";
  16.  
  17. function grabEmail($text) { //между названием функции и () не ставится пробел.
  18. $emailRegexp = '/[\w\.%_\+-]+@[\w\._-]+/i';
  19. $matches = [];
  20. $count = preg_match_all($emailRegexp, $text, $matches, PREG_SET_ORDER);
  21. foreach ($matches as $emails) {
  22. $mail[] = $emails[0];
  23. echo "$emails[0]\n";
  24. }
  25. return $mail;
  26. }
  27.  
  28. grabEmail($text);
Success #stdin #stdout 0.01s 20520KB
stdin
Standard input is empty
stdout
foo@demo.net
bar.ba@test.co.uk
mail@mail.ru
waka@wak.rc.uk
user0+test+hello_world@100500.com
user%office@company.com
BIGBOSS@BIGCOMPANY.COM