繁体   English   中英

在HTML5内的PHP中添加脚本

[英]Adding a script within HTML5 which is within php

我在以下代码中添加Google Adsense代码时遇到问题。

<?php
function ww_header(){
echo "<!DOCTYPE html>
<html>
<head>
<title>Page title</title>

// I Want to paste my Google Adsense code here

</head>
?> 

这就是我的Google Adsense代码的样子

<script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js">            
</script>
<script>
 (adsbygoogle = window.adsbygoogle || []).push({
      google_ad_client: "ca-pub-77XXXXXXXXXXX",
      enable_page_level_ads: true
 });
</script>

我尝试使用echo和include。 Adsense脚本与HTML5和php冲突,因为head标签位于php中。 有什么办法可以在不更改php和HTML位置的情况下编写Adsense代码。

问题是您正在使用echo " (带有双引号)。您的脚本代码也具有双引号。因此,您需要在script标记中转义双引号,或者将单引号用作您的echo 。您?

function ww_header(){
    echo '<!DOCTYPE html>
    <html>
    <head>
    <title>Page title</title>

    <script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js">            
    </script>
    <script>
     (adsbygoogle = window.adsbygoogle || []).push({
          google_ad_client: "ca-pub-77XXXXXXXXXXX",
          enable_page_level_ads: true
     });
    </script>

    </head>
    <body>
    Your content here...
    </body>
    </html>';
}
<?php
function ww_header()
{
    echo "<!DOCTYPE html>
<html>
<head>
<title>Page title</title>
<script async src='//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js'>            
        </script>
<script>
    (adsbygoogle = window.adsbygoogle || []).push({
      google_ad_client: 'ca-pub-77XXXXXXXXXXX',
      enable_page_level_ads: true
 });
</script>
</head>";

}
?>

可能有更好的方法来执行此操作,但是只是为了完全解决您的要求,请尝试以下操作:

<?php
function ww_header() {
    echo '<!DOCTYPE html>
          <html>
          <head>
           <title>Page title</title>

           <script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js">            
           </script>
           <script>
           (adsbygoogle = window.adsbygoogle || []).push({
            google_ad_client: "ca-pub-77XXXXXXXXXXX",
            enable_page_level_ads: true
            });
           </script>

           </head>';
?> 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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