AutoLinkEmails.php 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. <?php
  2. /**
  3. * @package koseven/Codebench
  4. * @category Tests
  5. * @author Geert De Deckere <geert@idoe.be>
  6. */
  7. class Bench_AutoLinkEmails extends Codebench {
  8. public $description =
  9. 'Fixing <a href="http://github.com/koseven/koseven/issues/2772">#2772</a>, and comparing some possibilities.';
  10. public $loops = 1000;
  11. public $subjects = [
  12. '<ul>
  13. <li>voorzitter@xxxx.com</li>
  14. <li>vicevoorzitter@xxxx.com</li>
  15. </ul>',
  16. ];
  17. // The original function, with str_replace replaced by preg_replace. Looks clean.
  18. public function bench_match_all_loop($subject)
  19. {
  20. if (preg_match_all('~\b(?<!href="mailto:|">|58;)(?!\.)[-+_a-z0-9.]++(?<!\.)@(?![-.])[-a-z0-9.]+(?<!\.)\.[a-z]{2,6}\b~i', $subject, $matches))
  21. {
  22. foreach ($matches[0] as $match)
  23. {
  24. $subject = preg_replace('!\b'.preg_quote($match).'\b!', HTML::mailto($match), $subject);
  25. }
  26. }
  27. return $subject;
  28. }
  29. // The "e" stands for "eval", hmm... Ugly and slow because it needs to reinterpret the PHP code upon each match.
  30. public function bench_replace_e($subject)
  31. {
  32. return preg_replace(
  33. '~\b(?<!href="mailto:|">|58;)(?!\.)[-+_a-z0-9.]++(?<!\.)@(?![-.])[-a-z0-9.]+(?<!\.)\.[a-z]{2,6}\b~ie',
  34. 'HTML::mailto("$0")', // Yuck!
  35. $subject
  36. );
  37. }
  38. // This one should be quite okay, it just requires an otherwise useless single-purpose callback.
  39. public function bench_replace_callback_external($subject)
  40. {
  41. return preg_replace_callback(
  42. '~\b(?<!href="mailto:|">|58;)(?!\.)[-+_a-z0-9.]++(?<!\.)@(?![-.])[-a-z0-9.]+(?<!\.)\.[a-z]{2,6}\b~i',
  43. [$this, '_callback_external'],
  44. $subject
  45. );
  46. }
  47. protected function _callback_external($matches)
  48. {
  49. return HTML::mailto($matches[0]);
  50. }
  51. // This one clearly is the ugliest, the slowest and consumes a lot of memory!
  52. public function bench_replace_callback_internal($subject)
  53. {
  54. return preg_replace_callback(
  55. '~\b(?<!href="mailto:|">|58;)(?!\.)[-+_a-z0-9.]++(?<!\.)@(?![-.])[-a-z0-9.]+(?<!\.)\.[a-z]{2,6}\b~i',
  56. function($matches)
  57. {
  58. return HTML::mailto($matches[0]);
  59. }, // Yuck!
  60. $subject
  61. );
  62. }
  63. }