fork download
  1. <?php
  2.  
  3. $text = "1. dog
  4. 1. cat
  5. 1. fish
  6. 1. horse
  7. 1. duck
  8. 1. goose
  9. 1. swan
  10. 1. monkey
  11. 1. chimpanzee
  12. 1. orangutan
  13. 1. whale
  14. 1. pig
  15. ";
  16.  
  17. function callback($match) {
  18. $out = preg_replace_callback("/(^($match[2] +)1\. .+(\\n|$))(?1)*/m", 'callback', $match[0]);
  19. $out = preg_replace("/^$match[2]1\. (.+)$/m", "<li>$1</li>", $out);
  20. return "<ol>\n$out</ol>\n";
  21. }
  22.  
  23. $html = preg_replace_callback("/(^( *)1\. .+(\\n|$))(?1)*/m", 'callback', $text);
  24.  
  25. echo $html;
  26.  
  27. ?>
Success #stdin #stdout 0.01s 20568KB
stdin
Standard input is empty
stdout
<ol>
<li>dog</li>
<li>cat</li>
<li>fish</li>
<ol>
<li>horse</li>
<ol>
<li>duck</li>
<ol>
<li>goose</li>
</ol>
<li>swan</li>
</ol>
<li>monkey</li>
<ol>
<li>chimpanzee</li>
<li>orangutan</li>
<li>whale</li>
</ol>
</ol>
<li>pig</li>
</ol>