Tokenize and Revert text - PHP Online

Form of PHP Sandbox

Enter Your PHP code here for testing/debugging in the Online PHP Sandbox. As in the usual PHP files, you can also add HTML, but do not forget to add the tag <?php in the places where the PHP script should be executed.



Your result can be seen below.

Result of php executing





Full code of Tokenize and Revert text.php

  1. <?php
  2.  
  3. $regex = "/((0?[1-9]|[12]\d|3[01])[\/](0?[1-9]|[12]\d|3[01])[\/](19|20)\d{2})/im";
  4.  
  5. $text = "12/12/2015   31/12/2019    caraota     12/12/2018";
  6.  
  7. // $message = preg_replace($regex, '', $text);
  8.  
  9. $datesTokens = [];
  10.  
  11. $out = preg_replace_callback(
  12.     $regex,
  13.     function($m) use (&$datesTokens) {
  14.         static $id = 0;
  15.         $id++;
  16.                 $datesTokens[] = $m[1];
  17.                 return "{date_".$id."}";
  18.     },
  19.     $text);
  20.  
  21. echo $text;
  22. echo "<br>";
  23. echo $out;
  24.  
  25. $out = preg_replace_callback(
  26.         '/{date_[1-9]}/mi',
  27.     function($m) use (&$datesTokens) {
  28.         static $id = -1;
  29.         $id++;
  30.                 return $datesTokens[$id];
  31.     },
  32.     $out);
  33.  
  34. echo "<br>";
  35. echo $out;
  36. echo "<br>";
File Description
  • Tokenize and Revert text
  • PHP Code
  • 12 Sep-2019
  • 664 Bytes
You can Share it: