我有html广告代码要插入到我的一个网站中。但我网站上的脚本使用了一个配置文件,广告代码应该在其中定义。我尝试过使用许多添加广告代码的技术,但都失败了。以下是我在脚本配置文件中的代码:
$config = array(
// Your Site URL
"url" => "http://www.example.com",
// Your Site Title
"title" => "Example.com",
// Your Site Description
"description" => "Description goes here",
// Google Analytics ID
"ga" => "",
// Ad Codes
"ad728" => "",
"ad468" => "",
"ad300" => "",
);
我的问题是,如何将广告代码包含在这些值中。我试着写一个单独的html文件,里面有广告代码,并尝试将其包含在这个变量中,但似乎什么都不起作用。输出是主页上的纯文本。
有几种方法可以做到这一点:
1) 仅escape
您的代码(在冲突的引号上使用反斜杠):
// This is probably the easiest thing to do (provided your script isn't massive).
$config['ad300'] = '<script>MyJavascript(''AddCodeValue'')</script>';
2) 交替串联。它很难看,但很管用!在使用单引号的地方,您总是必须使用双引号,反之亦然。
// Notice the double quotes wrapping single quotes here
$config['ad300'] = '<script>MyJavascript('."'AddCodeValue'".')</script>';
3) 使用HEREDOC
标记并在数组中分配变量。
$add1 = <<<EOF
<script>MyJavascript('AddCodeValue')</script>
EOF;
$config['ad300'] = $add1;
4) 将输出缓冲区与包含的文件或回显文本一起使用。
ob_start();
// Everything between start and end_clean
// whether it be include, code, whatever,
// will be saved into a cache essentially
include('ad728.php'); ?>
<script>
$('#myadd1').do(function() {
$("#add1_container").html("stuff");
});
</script>
<?php
// Once you are done with your code, you
// just save the contents of the cache (buffer)
// to a varaible
$add1 = ob_get_contents();
// This stops the buffer from caching
// and clears it out
ob_end_clean();
// Assign the variable to the array
$config['ad300'] = $add1;