简体   繁体   中英

How to shield a javascript string in php

I have a problem with my javascript string in php. So I tried to add a string to a variable in switch but I get error :

$blocPub = '<script type="text/javascript">
          var rdads=new String(Math.random()).substring (2, 11);
          document.write('<sc'+'ript type="text/javascript" src="http://test.com?sdsds?bhhj'+rdads+'dsdsdsdsds"></sc'+'ript>');
            </script>';

The error is in document.write. Exist a method to ecranate this string? Please help me!! Thx in advance.

您需要对引号内的引号进行转义。

$blocPub = '<script type="text/javascript">var rdads = new String(Math.random()).substring(2, 11);document.write(\'<script type="text/javascript" src="http://test.com?sdsds?bhhjbf=fdfdfd"></script>\'); </script>';

See if below works. You can either escape strings or alternate between "(double) and '(single) or do both in your case

case 7 :
        $blocPub = '<script type="text/javascript">
                        var rdads=new String(Math.random()).substring (2, 11);
                        document.write("<script type=\"text/javascript\" src=\"http://test.com?sdsds?bhhjbf=fdfdfd\"></script>");
                    </script>';
        break;
$blocPub = '<script type="text/javascript">
                        var rdads=new String(Math.random()).substring (2, 11);
                        document.write("<sc"+"ript type=\"text/javascript\" src="http://test.com?sdsds?bhhjbf=fdfdfd"></sc"+"ript>");
                    </script>';

Try this, i think you have mistake with quotes

$blocPub = '<script type="text/javascript">
                        var rdads=new String(Math.random()).substring (2, 11);
                        document.write("<script type="text/javascript" src="http://test.com?sdsds?bhhjbf=fdfdfd"/>");
                    </script>';

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM