Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Adsense parser WordPress

  • SOLVED

I need a simple PHP script which can convert strings of text including adsense blocks, such as the following code, into WordPress shortcodes of the form [adsense client="ca-pub-4209458083008072" slot="7550838362" width="125" height="135"].

jisd wj ewek j wekwh ewkejr wkej wer sd sd
<script type="text/javascript"><!--
google_ad_client = "ca-pub-4209458083008072";
/* test2 */
google_ad_slot = "7550838362";
google_ad_width = 125;
google_ad_height = 135;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>
32 ewiu hehr wejk sd 23w ejk2 jisd wj ewek

jisd wj ewek j wekwh ewkejr wkej wer sd sd
<script type="text/javascript"><!--
google_ad_client = "ca-pub-jweiov23v";
/* test3 */
google_ad_slot = "124124v";
google_ad_width = 250;
google_ad_height = 140;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>
32 ewiu hehr wejk sd 23w ejk2 jisd wj ewek


Answers (2)

2011-01-14

Utkarsh Kukreti answers:

<?php

add_shortcode('adsense', 'adsense_shortcode');
function adsense_shortcode($atts)
{
extract(shortcode_atts(array(
'client' => '',
'slot' => '',
'width' => '',
'height' => '',
), $atts));

echo <<<HTML
<script type="text/javascript"><!--
google_ad_client = "{$client}";
/* test2 */
google_ad_slot = "{$slot}";
google_ad_width = {$width};
google_ad_height = {$height};
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>
HTML;
}


Ryan Hellyer comments:

That's unfortunately just a WordPress shortcode which converts to the raw HTML. I was asking for the exact opposite, ie: to convert from the raw HTML to the shortcode.

What I'm trying to do, is to grab the content of a post, and convert all of the Google Adsense code blocks into shortcodes, hence I need to convert those JS blocks into [adsense] shortcodes. I've got the backend of the shortcode already sorted, I just messed up the parsing of the posts completed.

Thanks.


Utkarsh Kukreti comments:

Ah. Misread it completely :)


Utkarsh Kukreti comments:

<?php
$HTML = <<<HTML
<script type="text/javascript"><!--
google_ad_client = "ca-pub-4209458083008072";
/* test2 */
google_ad_slot = "7550838362";
google_ad_width = 125;
google_ad_height = 135;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>
HTML;

preg_match('/ad_client\s*=\s*"([^"]+?)"/', $HTML, $m);
$client = $m[1];
preg_match('/ad_slot\s*=\s*"([^"]+?)"/', $HTML, $m);
$slot = $m[1];
preg_match('/width\s*=\s*(\d+)/', $HTML, $m);
$width = $m[1];
preg_match('/height\s*=\s*(\d+)/', $HTML, $m);
$height = $m[1];

echo "[adsense client=\"$client\" slot=\"$slot\" width=\"$width\" height=\"$height\"]";


Should work as long as the format is similar.

2011-01-14

Nilesh shiragave answers:


function adsense_shortcode($atts) {
extract(shortcode_atts(array(
'client' => 'ca-pub-4209458083008072',
'slot' => '7550838362',
'width' => '125',
'height' => '135',
), $atts));

echo '<script type="text/javascript"><!--
google_ad_client = "'.$client.'";
/* test2 */
google_ad_slot = "'.$slot.'";
google_ad_width = '.$width.';
google_ad_height = '.$height.';
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>';
}
add_shortcode('adsense', 'adsense_shortcode');


Add above code inside your theme's functions.php file


Ryan Hellyer comments:

That's unfortunately just a WordPress shortcode which converts to the raw HTML. I was asking for the exact opposite, ie: to convert from the raw HTML to the shortcode.

What I'm trying to do, is to grab the content of a post, and convert all of the Google Adsense code blocks into shortcodes, hence I need to convert those JS blocks into [adsense] shortcodes. I've got the backend of the shortcode already sorted, I just messed up the parsing of the posts completed.

Thanks.