document.write来自mysql数据库的javascript并处理转义

I have some javascript code that I want to add to the page on load. This code has been saved to a mysql database 'as is'.

e.g. Google Adsense code or other Javascript snippets taken directly from the provider.

This is the code that I was using, but it is obviously replacing the wrong characters and not escaping when it should.

document.write("<?php echo str_replace('"',"'", preg_replace('#<\/script>#', '<\\/script>', trim(preg_replace('/\s+/', ' ', wordpressPlugin_get_option('source', 'field'))))); ?>");

How can I change the way I am parsing this retrieved string from the database so that I can add it to the page without the wrong characters being escaped?

Thanks for your help

Use this:

<script type="text/javascript">
    var code = "<code>&lt;?php {YOUR PHP CODE HERE} ?&gt;</code>";
    document.write(code);
</script>