Pulling name value pair from a structured adsense code block contained in a txt file
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";
//--&开发者_StackOverflowgt;
</script>
What about something like this :
$code = <<<STR
<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>
STR;
if (preg_match('/google_ad_client = "([^"]+)";/', $code, $m)) {
$google_ad_client = $m[1];
}
if (preg_match('/google_ad_slot = "([^"]+)";/', $code, $m)) {
$google_ad_slot = $m[1];
}
var_dump($google_ad_client, $google_ad_slot);
. And you'll get as output :
string 'pub-1234567890987654' (length=20)
string '1234567890' (length=10)
(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) :
if (preg_match('/google_ad_client\s*=\s*"([^"]+)"\s*;/', $code, $m)) {
$google_ad_client = $m[1];
}
if (preg_match('/google_ad_slot\s*=\s*"([^"]+)"\s*;/', $code, $m)) {
$google_ad_slot = $m[1];
}
And, just for fun, the same thing with only one regex, if you want to retrieve several pieces of informations :
$results = array();
if (preg_match_all('/(google_ad_client|google_ad_slot)\s*=\s*"([^"]+)"\s*;/', $code, $m)) {
$count = count($m[1]);
for ($i = 0 ; $i<$count ; $i++) {
$results[$m[1][$i]] = $m[2][$i];
}
}
// TODO : test is set (see isset) before using those
var_dump($results['google_ad_client'], $results['google_ad_slot']);
Which will give you the same output :
string 'pub-1234567890987654' (length=20)
string '1234567890' (length=10)
$data=file("file");
$d = preg_grep("/^google_ad_client|google_ad_slot/",$data);
print_r($d);
You can try this:
function fun($page) {
$result = array();
if(preg_match('{google_ad_client\s*=\s*"(.*?)"}',$page,$matches)) {
$result['google_ad_client'] = $matches[1];
}
if(preg_match('{google_ad_slot\s*=\s*"(.*?)"}',$page,$matches)) {
$result['google_ad_slot'] = $matches[1];
}
return $result;
}
精彩评论