在regex中需要帮助来检测谷歌转换ID

时间:2012-07-16 11:14:15

标签: php regex

我有一个脚本可以抓取转化和再营销代码,但我需要帮助来检测转化ID, 例如:

<!-- Google Code for current client Remarketing List -->
<script type="text/javascript">
/* <![CDATA[ */
var google_conversion_id = 1057577648; 
var google_conversion_language = "en";
var google_conversion_format = "3";
var google_conversion_color = "666666";
var google_conversion_label = "FJ8sCKCE9wEQsLWl-AM";
var google_conversion_value = 0;
/* ]]> */

需要这样的正则表达式:

$gaRegExp = "Regex Here";

preg_match( $gaRegExp , $str , $matches);

$conversion_id = $matches;       

1 个答案:

答案 0 :(得分:0)

试试这个

$subject = 'var google_conversion_id = 1057577648; 
var google_conversion_language = "en";
var google_conversion_format = "3";
var google_conversion_color = "666666";
var google_conversion_label = "FJ8sCKCE9wEQsLWl-AM";
var google_conversion_value = 0;';
$pattern = '!(?<=google_conversion_id\s\=\s)\d+!';
preg_match($pattern, $subject, $matches);
print_r($matches);

输出:

Array ( [0] => 1057577648 ) 

获取元素$conversion_id=$matches[0];