Php 将ad代码作为变量添加到数组中

Php 将ad代码作为变量添加到数组中,php,arrays,Php,Arrays,我有html广告代码插入到我的网站之一。但是我网站上的脚本使用了一个配置文件,广告代码应该在其中定义。我尝试过使用许多技术在其中添加广告代码,但都失败了。下面是我在脚本的配置文件中的代码: $config = array( // Your Site URL "url" => "http://www.example.com", // Your Site Title "title" => "Example.com", // Your Site De

我有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
您的代码(在冲突的引号上使用反斜杠):

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;
ob_start();
//从开始到结束的一切都很干净
//无论是包含,代码,还是其他,
//将被保存到缓存中
包括('ad728.php');?>
$('#myadd1').do(函数(){
$(“#add1_container”).html(“stuff”);
});

因此,在数组已经定义之后,您需要将html插入到这个数组中?是的,我认为脚本是这样工作的,这就是为什么这个配置文件中有未定义的ad代码变量。如果我能理解如何做到这一点。那么为什么你不能在这个配置数组中的
“ad728”=>“stuff”
引号中编写html呢?我尝试使用的广告代码中包含两个标记。脚本开始在第17行的/home/vhosts/example.mn/example.com/includes/Config.php中抛出以下错误:`Parse error:syntax error,意外的'text'(T_STRING),应为''`。我创建了另一个名为ad728.html的文件,并将整个广告代码放在其中,然后将该文件包含在带有src属性的script标记中,如下所示:但它也给出了相同的意外“text”(T_STRING)错误。不过,您的herdeoc没有正确关闭。EOF;应放置在带有任何space@DarkBee谢谢你的留言,我从来没有使用过herdoc(我想这说明了!)。我会修改的。
// Notice the double quotes wrapping single quotes here
$config['ad300']  =  '<script>MyJavascript('."'AddCodeValue'".')</script>';
$add1  =  <<<EOF
          <script>MyJavascript('AddCodeValue')</script>
EOF;

$config['ad300']  =  $add1;
    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;