file_put_contents插入javascript" "破坏关闭

时间:2014-03-24 12:33:28

标签: javascript php

如何将js脚本放入file_put_content

$videonova_content = "
           var InitialScriptApi = "<script src ='http://www.youtube.com/player_api'></script>";
           var youtubeTitle = "Wordpress";
           var youtubeSource = "fH6B4S9ENY4";
           var youtubeControl = "1";
           var youtubeAutoplay = "0";
           var youtubeDisplay = 7;
             var youtubeForcegrab = '0';
             ";

       file_put_contents($file, $videonova_content);

问题是插入js时。  &#34; &#34;破坏封闭。怎么解决这个?插入&#34; &#39;&#34;&#34;&#39; &#34;如何封装?

2 个答案:

答案 0 :(得分:2)

首先,我的脚本甚至不会在PHP中运行,你会得到和语法错误,所以我真的没有看到你如何测试你的脚本。

正确修复当前脚本的方法如下:

$videonova_content = "
var InitialScriptApi = \"<script src ='http://www.youtube.com/player_api'></script>\";
var youtubeTitle = \"Wordpress\";
var youtubeSource = \"fH6B4S9ENY4\";
var youtubeControl = \"1\";
var youtubeAutoplay = \"0\";
var youtubeDisplay = 7;
var youtubeForcegrab = '0';
";

file_put_contents($file, $videonova_content);

为此,您可以在http://www.php.net/manual/en/language.types.string.php

阅读更多内容

然而,更好的方法是使用HEREDOC或NOWDOC,因为它们的目的正是您所需要的。所以,在你的情况下,它会是这样的:

$videonova_content = <<<SCRIPT
var InitialScriptApi = "<script src ='http://www.youtube.com/player_api'></script>";
var youtubeTitle = "Wordpress";
var youtubeSource = "fH6B4S9ENY4";
var youtubeControl = "1";
var youtubeAutoplay = "0";
var youtubeDisplay = 7;
var youtubeForcegrab = '0';
SCRIPT;
file_put_contents($file, $videonova_content);

您可以在http://www.php.net/manual/en/language.types.string.php#language.types.string.syntax.heredoc

了解更多有关HEREDOC的信息

答案 1 :(得分:0)

我更喜欢这个,因为你可以缩进这个,这与HEREDOC方法不同。

<?php ob_start(); ?>
var InitialScriptApi = "<script src ='http://www.youtube.com/player_api'></script>";
var youtubeTitle = "Wordpress";
var youtubeSource = "fH6B4S9ENY4";
var youtubeControl = "1";
var youtubeAutoplay = "0";
var youtubeDisplay = 7;
var youtubeForcegrab = '0';
<?   file_put_contents($file, ob_get_clean() ); ?>