Php 从txt文件中包含的结构化adsense代码块中提取名称-值对

Php 从txt文件中包含的结构化adsense代码块中提取名称-值对,php,regex,Php,Regex,我有一个txt文件,其中包含一个google adsense代码块,我正试图通过file_get_内容拉入该文件,以提取google_ad_客户端和google_ad_插槽变量的值 在下面的示例中,我想返回调用函数: $google_ad_client = 'pub-1234567890987654'; $google_ad_slot = '1234567890' 该文件可能包含这两种格式之一,我不知道用户选择了哪种格式: 更新的广告单元样式 <script type="text/jav

我有一个txt文件,其中包含一个google adsense代码块,我正试图通过file_get_内容拉入该文件,以提取google_ad_客户端和google_ad_插槽变量的值

在下面的示例中,我想返回调用函数:

$google_ad_client = 'pub-1234567890987654';
$google_ad_slot = '1234567890'
该文件可能包含这两种格式之一,我不知道用户选择了哪种格式:

更新的广告单元样式

<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>

古典风格

<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> 

像这样的东西怎么样:

$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);
(使用代码的第二部分进行测试,似乎也可以正常工作)


作为预防措施,您可能还希望将正则表达式更改为类似的形式,因此接受不带任何空格(或带任何数量空格)的同类代码:


而且,为了好玩,如果您想检索多条信息,只需使用一个正则表达式即可:

$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']);
您可以尝试以下方法:

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;
}
伙计,你们这些雷鬼小子真让我吃惊。美好的
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];
}
$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']);
string 'pub-1234567890987654' (length=20)
string '1234567890' (length=10)
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;
}