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>
What about something like this :
.
And you’ll get as output :
(Tested with the second portion of code, and seems to work OK too)
Just as a precaution, you might also want to change the regexes to something like this, so accept the same kind of code without any space (or with any number of spaces) :
And, just for fun, the same thing with only one regex, if you want to retrieve several pieces of informations :
Which will give you the same output :