We Recommend

Wicked Cool PHP: Real-World Scripts That Solve Difficult Problems Wicked Cool PHP: Real-World Scripts That Solve Difficult Problems
Wicked Cool PHP contains a wide variety of scripts to process credit cards, check the validity of email addresses, template HTML, and serve dynamic images and text.


Posted By

section31 on 08/31/08


Tagged


Versions (?)


Who likes this?

2 people have marked this snippet as a favorite

jdstraughan
JimiJay


To Javascript String


Published in: PHP 


  1. /*
  2.  
  3. // example usage
  4.  
  5. <script type="text/javascript">
  6. var str = '<?= to_js_string($html) ?>';
  7. </script>
  8.  
  9. */
  10.  
  11. function to_js_string($str) {
  12. return str_replace(array("\\", "'", "\r", "\n"), array('\\\\', "\'", ' ', ' '), $str);
  13. }

Report this snippet 

You need to login to post a comment.