如何使用preg_match自动将常规Google AdSense广告转换为响应广告

How to convert a regular Google AdSense Ad in a responsive ad automatically using preg_match?

本文关键字:Google AdSense 转换 响应 常规 preg 何使用 match      更新时间:2023-09-26
  • WordPress主题选项面板
  • 一个文本区域字段,用户在其中输入其无响应的AdSense代码

我需要,如果用户输入一个非响应的adsense代码,该代码将转换为响应代码如下:

无扩展代码:

<script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<!-- non responsive ads -->
<ins class="adsbygoogle"
     style="display:inline-block;width:728px;height:90px"
     data-ad-client="ca-pub-12345"
     data-ad-slot="xxxxxxxxxxxxxx"></ins>
<script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script>

上述代码需要在中转换

<script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<ins class="adsbygoogle"
style="display:block"
data-ad-client="ca-pub-12345"
data-ad-slot="xxxxxxxxxxxxxx"
data-ad-format="auto"></ins>
<script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script>

我想这项工作可以用preg_match完成,但怎么做呢?

谢谢。

你不能那样做(据我所知)。

该(无响应)单元配置存储在谷歌服务器上

{
  "status": "ACTIVE", 
  "kind": "adsense#adUnit", 
  "code": "xxxxxxxxxxxxxx", 
  "name": "non responsive ads", 
  "savedStyleId": "qwertzuiopšđžasdfghjklyxcvbnmqwerfvbgtzhnmjgbvfredcxsw", 
  "contentAdsSettings": {
    "type": "TEXT_IMAGE", 
    "backupOption": {
      "type": "BLANK"
    }, 
    "size": "SIZE_728_90"
  }, 
  "id": "ca-pub-12345:xxxxxxxxxxxxxx"
}

pagead2.googlesyndication.com会将data-ad-slot="xxxxxxxxxxxxxx"广告请求视为对SIZE_728_90格式的请求,并且你不能"随时"更改这一点——如果你需要另一个size,你需要创建另一个广告单元。

即使你会这样做,而且看起来"有效",这仍然违反了AdSense政策,这将对你网站上所有涉及的AdSense账户和广告服务构成风险。(因为"转换"后的代码会使你产生一定百分比的无效广告印象和/或糟糕的请求。)

AdSense广告代码的修改
https://support.google.com/adsense/answer/1354736?hl=en

创建响应式广告单元
https://support.google.com/adsense/answer/3543893?hl=en