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

Posted on 2014-10-14
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;
<!-- ap-sm-banner -->
<script type="text/javascript"

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

<script async src="//"></script>
<!-- ap-sm-banner -->
<ins class="adsbygoogle"
     data-ad-client="insert-pub-id-string-here"  //pub string
     data-ad-slot="insert-ad-slot-string-here">  //slot string
(adsbygoogle = window.adsbygoogle || []).push({});


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?


Question by:St_Aug_Beach_Bum
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2

Expert Comment

by:Ahmed Merghani
ID: 40381497

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?
LVL 35

Accepted Solution

Terry Woods earned 500 total points
ID: 40381605
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 {
if (preg_match('#google_ad_slot\s*=\s*"(.*?)"#', $old_ad_code, $match)) {
  $google_ad_slot = $match[1];
} else {

print <<<EOS
<script async src="//"></script>
<!-- ap-sm-banner -->
<ins class="adsbygoogle"
(adsbygoogle = window.adsbygoogle || []).push({});

Open in new window

LVL 35

Expert Comment

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

Author Closing Comment

ID: 40382018
Awesome - thank you very much!

Featured Post

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
php output utf-8 problem 6 51
Horizontal Full Calendar using php 5 42
Google Recaptcha 13 51
Link Stopped Working 7 30
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 i…
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to dynamically set the form action using jQuery.

739 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