Solved

Need help with preg_replace script to change adsense ads from sync to async

Posted on 2014-10-14
4
222 Views
Last Modified: 2014-10-15
I do adsense sharing on my site.  My users store their adsense code in my db.

I need to use the newer async adsense ads on the site, but some users have the old sync code saved in the db.

So my goal here is to take the string that contains the old sync code, remove the publisher id and slot number, and insert those into the async code, as the page is made.

Ok, I've got:

//-----------------------

$old_ad_code = '<script type="text/javascript">
    google_ad_client = "pub-id-1234567";  //need the string within the quotes
    google_ad_slot = "slot-123";  //need the string between the quotes
    google_ad_width = 468;
    google_ad_height = 60;
</script>
<!-- ap-sm-banner -->
<script type="text/javascript"
src="//pagead2.googlesyndication.com/pagead/show_ads.js">
</script>';

//-----------
//then need to insert those strings into the async code
//----------

<script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<!-- ap-sm-banner -->
<ins class="adsbygoogle"
     style="display:inline-block;width:468px;height:60px"
     data-ad-client="insert-pub-id-string-here"  //pub string
     data-ad-slot="insert-ad-slot-string-here">  //slot string
</ins>  
<script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script>

//--------------------------


ok, suppose I could use explode on this?  

But it seems like preg_replace might be better?  But I have no idea how to use that really.

Can you show me how to do this?

Thanks,

Chris
0
Comment
Question by:St_Aug_Beach_Bum
  • 2
4 Comments
 
LVL 8

Expert Comment

by:Ahmed Merghani
Comment Utility
Hello,

Can you be more specific so we can advise regarding?
What do you want to convert for example "pub-id-1234567" to?
What do you want to convert for example "slot-123" to?
0
 
LVL 35

Accepted Solution

by:
Terry Woods earned 500 total points
Comment Utility
You should be able to do something like this:
if (preg_match('#google_ad_client\s*=\s*"(.*?)"#', $old_ad_code, $match)) {
  $google_ad_client = $match[1];
} else {
  #error
}
if (preg_match('#google_ad_slot\s*=\s*"(.*?)"#', $old_ad_code, $match)) {
  $google_ad_slot = $match[1];
} else {
  #error
}

print <<<EOS
<script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<!-- ap-sm-banner -->
<ins class="adsbygoogle"
     style="display:inline-block;width:468px;height:60px"
     data-ad-client="$google_ad_client" 
     data-ad-slot="$google_ad_slot">
</ins>  
<script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script>
EOS;

Open in new window

0
 
LVL 35

Expert Comment

by:Terry Woods
Comment Utility
You could do it with a single preg_replace, but it's probably easier to understand (and thus maintain) when kept separate.
0
 

Author Closing Comment

by:St_Aug_Beach_Bum
Comment Utility
Awesome - thank you very much!
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now