Pulling name value pair from a structured adsense code block contained in a txt file

Posted by Scott B on Stack Overflow See other posts from Stack Overflow or by Scott B
Published on 2010-03-09T06:28:00Z Indexed on 2010/03/09 6:36 UTC
Read the original article Hit count: 216

Filed under:
|

I have a txt file which contains a google adsense code block and I'm trying to pull in the file via file_get_contents to extract the values of the google_ad_client and google_ad_slot variables.

In the examples below, I want to return to my calling function:

$google_ad_client = 'pub-1234567890987654';
$google_ad_slot = '1234567890'

The file may contain one of either of these two formats and I wont know which the user has chosen:

Newer Ad Unit Style

<script type="text/javascript"><!--
google_ad_client = "pub-1234567890987654";
google_ad_slot = "1234567890";
google_ad_width = 336;
google_ad_height = 280;
//-->
</script> 
<script type="text/javascript" src="path-to-google-script"></script>

Classic Style

<script type="text/javascript"><!--
google_ad_client = "pub-1234567890987654";
/* 336x280, created 8/6/09 */
google_ad_slot = "1234567890";
google_ad_width = 336;
google_ad_height = 280;
google_ad_format="336x280_as";
google_ad_type="text_image";
google_color_border="FFFFFF";
google_color_bg="FFFFFF";
google_color_link="2200CC";
google_color_url="000000";
google_color_text="777777";
//-->
</script> 

© Stack Overflow or respective owner

Related posts about php

Related posts about regex