Friday, September 23, 2011

PHP: Output any string using JavaScript

Output any string from PHP using JavaScript document.write() function.

 
function echoWithJS($_str) {
    $str = str_replace(
        array('"', '</', chr(10), chr(11), chr(13)),
        array('\\"', '<\\/', '', '', ''),
        $_str
    );
    $out = '
        <script language="javascript" type="text/javascript">
            //<!--
            '.'document.write("'.$str.'");
            //-->
        </script>
    ';
    echo $out;
}


No comments: