Chris Andrews
asked on
Need help with preg_replace script to change adsense ads from sync to async
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.googlesyndi cation.com /pagead/sh ow_ads.js" >
</script>';
//-----------
//then need to insert those strings into the async code
//----------
<script async src="//pagead2.googlesyndi cation.com /pagead/js /adsbygoog le.js"></s cript>
<!-- ap-sm-banner -->
<ins class="adsbygoogle"
style="display:inline-bloc k;width:46 8px;height :60px"
data-ad-client="insert-pub -id-string -here" //pub string
data-ad-slot="insert-ad-sl ot-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
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.googlesyndi
</script>';
//-----------
//then need to insert those strings into the async code
//----------
<script async src="//pagead2.googlesyndi
<!-- ap-sm-banner -->
<ins class="adsbygoogle"
style="display:inline-bloc
data-ad-client="insert-pub
data-ad-slot="insert-ad-sl
</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
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
You could do it with a single preg_replace, but it's probably easier to understand (and thus maintain) when kept separate.
ASKER
Awesome - thank you very much!
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?