fork download
  1. <?php
  2.  
  3. function escape_as_double_quoted_literal_matcher ($m) {
  4. $ch = $m[0];
  5. switch ($ch) {
  6. case "\n": return '\n';
  7. case "\r": return '\r';
  8. case "\t": return '\t';
  9. case "\v": return '\v';
  10. case "\e": return '\e';
  11. case "\f": return '\f';
  12. case "\\": return '\\\\';
  13. case "\$": return '\\$';
  14. case "\"": return '\\"';
  15. case "\0": return '\0';
  16. default:
  17. $h = dechex(ord($ch));
  18. return '\x' . (strlen($h) > 1 ? $h : '0' . $h);
  19. }
  20. }
  21.  
  22. function escape_as_double_quoted_literal ($val) {
  23. "|[^\x20-\x21\x23\x25-\x5b\x5e-\x7e]|",
  24. "escape_as_double_quoted_literal_matcher",
  25. $val);
  26. }
  27.  
  28. $text = "\0\1\xff\"hello\\world\"\n\$";
  29. echo escape_as_double_quoted_literal($text);
Success #stdin #stdout 0.01s 20520KB
stdin
Standard input is empty
stdout
\0\x01\xff\"hello\\world\"\n\$