fork download
  1. <?php
  2.  
  3. $string = '
  4. <!--
  5. this is a comment
  6.  
  7. -->
  8.  
  9. <br/> <br>
  10.  
  11. <!--
  12. this is a comment
  13. -->
  14. <br/> <br/>
  15. <!--
  16. this is a comment
  17.  
  18. -->
  19. Hello world';
  20.  
  21. $new_string = preg_replace_callback('#(?(?!<!--).*?(?=<!--|$)|(<!--.*?-->))#s', function($m){
  22. if(!isset($m[1])){
  23. return preg_replace('#\s+#s', ' ', $m[0]);
  24. }
  25. return $m[0];
  26. },$string);
  27.  
  28. echo $new_string;
  29. ?>
Success #stdin #stdout 0.01s 20568KB
stdin
Standard input is empty
stdout
 <!--
this   is a comment

--> <br/> <br> <!--
this   is a comment
--> <br/> <br/> <!--
this   is a comment

--> Hello world