2012-04-16 19 views
-1

你好我有一个php代码抓取工具来检测一个网站,如果它有一个异步谷歌代码。 这里检测到的非同步谷歌代码片段:抓取代码的爬虫异步遗传算法代码

$async_ga_string = "ga.async"; 

if(!strpos($str, $async_ga_string))$async_ga = ''; 
      else $async_ga = 'yes'; 

它工作正常。但我究竟需要的是对我来说,能够得到非同步UA码在此谷歌代码片段:

 var _gaq = _gaq || []; 
    _gaq.push(['_setAccount', 'UA-2595901-1']); 
    _gaq.push(['_trackPageview']); 

我需要得到“UA-2595901-1”

+0

真的不知道该怎么做才知道。还在搞清楚自己。感谢您的快速回复@xbonez – jalf 2012-04-16 01:07:19

回答

0

这是一个完美的解决方案,但Google Analytics帐户ID几乎可以通过简单的正则表达式找到它:

<?php 
$gaRegExp = '/UA-\d+-\d+/'; 

$testText = "var _gaq = _gaq || []; 
    _gaq.push(['_setAccount', 'UA-2595901-1']); 
    _gaq.push(['_trackPageview']);" 

preg_match($gaRegExp , $testText , $matches) 

// Returns, within the $matches variable, the following 
// array (
// 0 => 'UA-2595901-1', 
//) 
+0

谢谢!我现在就试试 – jalf 2012-04-16 01:58:10

+0

$匹配变量来自哪里? – jalf 2012-04-16 02:17:46

+0

它的工作原理!感谢lucanos! – jalf 2012-04-16 02:35:55