extract-email-links.php 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. <?php
  2. /**
  3. * dolphin. Collection of useful PHP skeletons.
  4. * Copyright (C) 2019 Johannes 'Banana' Keßler
  5. *
  6. * This program is free software: you can redistribute it and/or modify
  7. * it under the terms of the COMMON DEVELOPMENT AND DISTRIBUTION LICENSE
  8. *
  9. * You should have received a copy of the
  10. * COMMON DEVELOPMENT AND DISTRIBUTION LICENSE (CDDL) Version 1.0
  11. * along with this program. If not, see http://www.sun.com/cddl/cddl.html
  12. */
  13. /**
  14. * extract from given string (was email body) any links we want to add
  15. * should be in the right format
  16. * return an array with links and the infos about them
  17. *
  18. * @param string $string
  19. * @return array $ret
  20. */
  21. static function extractEmailLinks($string) {
  22. $ret = array();
  23. #this matches a valid URL. An URL with | is still valid...
  24. $urlpattern = '#(?i)\b((?:[a-z][\w-]+:(?:/{1,3}|[a-z0-9%])|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}/)(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:\'".,<>?«»“”‘’]))#';
  25. preg_match_all($urlpattern, $string, $matches);
  26. if(isset($matches[0]) && !empty($matches[0])) {
  27. foreach($matches[0] as $match) {
  28. $ret[md5($match)] = $match;
  29. }
  30. }
  31. return $ret;
  32. }