fork download
  1. <?php
  2. function do_replace($string) {
  3. $regex = '/^(\((?:<([a-z])>)?(\d{0,3}|[a-z]{1,3})(?:<\/\2>)?(\.)?\)|\[(?:<([a-z])>)?(\d{0,3}|[a-z]{1,3})(?:<\/\2>)?(\.)?\])\s*(.*)/i';
  4. $result = preg_match($regex, $string);
  5. if($result) {
  6. return preg_replace($regex, '%%$1|$8', $string);
  7. } else {
  8. $regex = '/^(\d{0,3}|[a-z]{1,3})\.\s*(.+)$/i';
  9. $result = preg_match($regex, $string);
  10. if($result) {
  11. return preg_replace($regex, '%%$1.|$2', $string);
  12. } else {
  13. return $string;
  14. }
  15. }
  16. }
  17. $strings = array(
  18. '(1)blahblah',
  19. '(<i>iv</i>.) blahblah',
  20. '[b] &nbsp;some stuff',
  21. '25. blahblah',
  22. 'A. some other stuff. one',
  23. 'blah. some other stuff',
  24. 'text (1) text',
  25. '2008. blah',
  26. '[123) <-- mismatch'
  27. );
  28. foreach($strings as $string) echo do_replace($string) . PHP_EOL;
  29. ?>
Success #stdin #stdout 0.02s 13112KB
stdin
Standard input is empty
stdout
%%(1)|blahblah
%%(<i>iv</i>.)|blahblah
%%[b]|&nbsp;some stuff
%%25.|blahblah
%%A.|some other stuff. one
blah. some other stuff
text (1) text
2008. blah
[123) <-- mismatch