如何 preg_match() 来自 file_get_contents() 的变量
How to preg_match() a variable from file_get_contents()
我创建了一个非常基本的脚本,它从具有 PHP 的 url 获取数据(对于 Google 趋势):
$url = 'https://www.google.com/trends/fetchComponent?hl=en-US&q=battlefield%201&cid=TIMESERIES_GRAPH_0&export=5&w=500&h=300&gprop=youtube&date=today%201-m';
$url2 = file_get_contents($url);
在源代码中我想提取某种类型的数据
{"columns":[{"id":"d","label":"Date","type":"datetime"},{"role":"annotation","type":"string"},{"p":{"html":true},"role":"annotationText","type":"string"},{"id":"q0","label":"battlefield 1","type":"number"},{"role":"annotation","type":"string"},{"p":{"html":true},"role":"annotationText","type":"string"},{"role":"certainty","type":"boolean"}],"headlineDataPoints":[],"width":485,"axisAnnotations":[],"rows":[[{"v":new Date(2016, 8, 24, 12, 0),"f":"Saturday, September 24, 2016"},null,null,21,null,null,true],[{"v":new Date(2016, 8, 25, 12, 0),"f":"Sunday, September 25, 2016"},null,null,23,null,null,true],[{"v":new Date(2016, 8, 26, 12, 0),"f":"Monday, September 26, 2016"},null,null,19,null,null,true],[{"v":new Date(2016, 8, 27, 12, 0),"f":"Tuesday, September 27, 2016"},null,null,44,null,null,true],[{"v":new Date(2016, 8, 28, 12, 0),"f":"Wednesday, September 28, 2016"},null,null,54,null,null,true],[{"v":new Date(2016, 8, 29, 12, 0),"f":"Thursday, September 29, 2016"},null,null,39,null,null,true],[{"v":new Date(2016, 8, 30, 12, 0),"f":"Friday, September 30, 2016"},null,null,35,null,null,true],[{"v":new Date(2016, 9, 1, 12, 0),"f":"Saturday, October 1, 2016"},null,null,38,null,null,true],[{"v":new Date(2016, 9, 2, 12, 0),"f":"Sunday, October 2, 2016"},null,null,64,null,null,true],[{"v":new Date(2016, 9, 3, 12, 0),"f":"Monday, October 3, 2016"},null,null,46,null,null,true],[{"v":new Date(2016, 9, 4, 12, 0),"f":"Tuesday, October 4, 2016"},null,null,35,null,null,true],[{"v":new Date(2016, 9, 5, 12, 0),"f":"Wednesday, October 5, 2016"},null,null,34,null,null,true],[{"v":new Date(2016, 9, 6, 12, 0),"f":"Thursday, October 6, 2016"},null,null,34,null,null,true],[{"v":new Date(2016, 9, 7, 12, 0),"f":"Friday, October 7, 2016"},null,null,31,null,null,true],[{"v":new Date(2016, 9, 8, 12, 0),"f":"Saturday, October 8, 2016"},null,null,29,null,null,true],[{"v":new Date(2016, 9, 9, 12, 0),"f":"Sunday, October 9, 2016"},null,null,30,null,null,true],[{"v":new Date(2016, 9, 10, 12, 0),"f":"Monday, October 10, 2016"},null,null,31,null,null,true],[{"v":new Date(2016, 9, 11, 12, 0),"f":"Tuesday, October 11, 2016"},null,null,22,null,null,true],[{"v":new Date(2016, 9, 12, 12, 0),"f":"Wednesday, October 12, 2016"},null,null,40,null,null,true],[{"v":new Date(2016, 9, 13, 12, 0),"f":"Thursday, October 13, 2016"},null,null,63,null,null,true],[{"v":new Date(2016, 9, 14, 12, 0),"f":"Friday, October 14, 2016"},null,null,55,null,null,true],[{"v":new Date(2016, 9, 15, 12, 0),"f":"Saturday, October 15, 2016"},null,null,71,null,null,true],[{"v":new Date(2016, 9, 16, 12, 0),"f":"Sunday, October 16, 2016"},null,null,64,null,null,true],[{"v":new Date(2016, 9, 17, 12, 0),"f":"Monday, October 17, 2016"},null,null,84,null,null,true],[{"v":new Date(2016, 9, 18, 12, 0),"f":"Tuesday, October 18, 2016"},null,null,100,null,null,true],[{"v":new Date(2016, 9, 19, 12, 0),"f":"Wednesday, October 19, 2016"},null,null,null,null,null,true],[{"v":new Date(2016, 9, 20, 12, 0),"f":"Thursday, October 20, 2016"},null,null,null,null,null,true],[{"v":new Date(2016, 9, 21, 12, 0),"f":"Friday, October 21, 2016"},null,null,null,null,null,true],[{"v":new Date(2016, 9, 22, 12, 0),"f":"Saturday, October 22, 2016"},null,null,null,null,null,true],[{"v":new Date(2016, 9, 23, 12, 0),"f":"Sunday, October 23, 2016"},null,null,null,null,null,true]],"showHeadlines":false,"percentData":false,"colors":["#3f85f2"],"height":230}
如何使用 preg_match 找到它?我试过了
$data = preg_match('/^(var chartData = |; var htmlChart)\/$/', $url2, $output_array);
print_r($output_array);
我使用 first 和 last 关键字来尝试获取它们之间的内容(成为我想要的数据)
我试过使用 phpliveregex,它实际上确实可以获取我想要的东西(找到 var chartData 字符串)http://www.phpliveregex.com/p/hCB 但是当我尝试克隆它时它不起作用。
我的问题是,我如何使用 preg_match() 从 Google 趋势中提取此 json 对象(在 chartData 变量内),因为我尝试过的还没有一直在工作。
问题似乎来自 "JSON string" 中的几个值,这些值看起来都像 new Date(2016, 9, 14, 12, 0)
,包含空格且未包含在引号之间。您可以解决在它们周围添加引号的问题:
$str = preg_replace('~:(new Date\([^)]*\))~', ':""', $str);
$jsonArray = json_decode($str, true);
这应该会有帮助
preg_match('/chartData\s+=\s+\{(.+)\}/', $url2, $output_array);
$charData = $output_array[0];
我创建了一个非常基本的脚本,它从具有 PHP 的 url 获取数据(对于 Google 趋势):
$url = 'https://www.google.com/trends/fetchComponent?hl=en-US&q=battlefield%201&cid=TIMESERIES_GRAPH_0&export=5&w=500&h=300&gprop=youtube&date=today%201-m';
$url2 = file_get_contents($url);
在源代码中我想提取某种类型的数据
{"columns":[{"id":"d","label":"Date","type":"datetime"},{"role":"annotation","type":"string"},{"p":{"html":true},"role":"annotationText","type":"string"},{"id":"q0","label":"battlefield 1","type":"number"},{"role":"annotation","type":"string"},{"p":{"html":true},"role":"annotationText","type":"string"},{"role":"certainty","type":"boolean"}],"headlineDataPoints":[],"width":485,"axisAnnotations":[],"rows":[[{"v":new Date(2016, 8, 24, 12, 0),"f":"Saturday, September 24, 2016"},null,null,21,null,null,true],[{"v":new Date(2016, 8, 25, 12, 0),"f":"Sunday, September 25, 2016"},null,null,23,null,null,true],[{"v":new Date(2016, 8, 26, 12, 0),"f":"Monday, September 26, 2016"},null,null,19,null,null,true],[{"v":new Date(2016, 8, 27, 12, 0),"f":"Tuesday, September 27, 2016"},null,null,44,null,null,true],[{"v":new Date(2016, 8, 28, 12, 0),"f":"Wednesday, September 28, 2016"},null,null,54,null,null,true],[{"v":new Date(2016, 8, 29, 12, 0),"f":"Thursday, September 29, 2016"},null,null,39,null,null,true],[{"v":new Date(2016, 8, 30, 12, 0),"f":"Friday, September 30, 2016"},null,null,35,null,null,true],[{"v":new Date(2016, 9, 1, 12, 0),"f":"Saturday, October 1, 2016"},null,null,38,null,null,true],[{"v":new Date(2016, 9, 2, 12, 0),"f":"Sunday, October 2, 2016"},null,null,64,null,null,true],[{"v":new Date(2016, 9, 3, 12, 0),"f":"Monday, October 3, 2016"},null,null,46,null,null,true],[{"v":new Date(2016, 9, 4, 12, 0),"f":"Tuesday, October 4, 2016"},null,null,35,null,null,true],[{"v":new Date(2016, 9, 5, 12, 0),"f":"Wednesday, October 5, 2016"},null,null,34,null,null,true],[{"v":new Date(2016, 9, 6, 12, 0),"f":"Thursday, October 6, 2016"},null,null,34,null,null,true],[{"v":new Date(2016, 9, 7, 12, 0),"f":"Friday, October 7, 2016"},null,null,31,null,null,true],[{"v":new Date(2016, 9, 8, 12, 0),"f":"Saturday, October 8, 2016"},null,null,29,null,null,true],[{"v":new Date(2016, 9, 9, 12, 0),"f":"Sunday, October 9, 2016"},null,null,30,null,null,true],[{"v":new Date(2016, 9, 10, 12, 0),"f":"Monday, October 10, 2016"},null,null,31,null,null,true],[{"v":new Date(2016, 9, 11, 12, 0),"f":"Tuesday, October 11, 2016"},null,null,22,null,null,true],[{"v":new Date(2016, 9, 12, 12, 0),"f":"Wednesday, October 12, 2016"},null,null,40,null,null,true],[{"v":new Date(2016, 9, 13, 12, 0),"f":"Thursday, October 13, 2016"},null,null,63,null,null,true],[{"v":new Date(2016, 9, 14, 12, 0),"f":"Friday, October 14, 2016"},null,null,55,null,null,true],[{"v":new Date(2016, 9, 15, 12, 0),"f":"Saturday, October 15, 2016"},null,null,71,null,null,true],[{"v":new Date(2016, 9, 16, 12, 0),"f":"Sunday, October 16, 2016"},null,null,64,null,null,true],[{"v":new Date(2016, 9, 17, 12, 0),"f":"Monday, October 17, 2016"},null,null,84,null,null,true],[{"v":new Date(2016, 9, 18, 12, 0),"f":"Tuesday, October 18, 2016"},null,null,100,null,null,true],[{"v":new Date(2016, 9, 19, 12, 0),"f":"Wednesday, October 19, 2016"},null,null,null,null,null,true],[{"v":new Date(2016, 9, 20, 12, 0),"f":"Thursday, October 20, 2016"},null,null,null,null,null,true],[{"v":new Date(2016, 9, 21, 12, 0),"f":"Friday, October 21, 2016"},null,null,null,null,null,true],[{"v":new Date(2016, 9, 22, 12, 0),"f":"Saturday, October 22, 2016"},null,null,null,null,null,true],[{"v":new Date(2016, 9, 23, 12, 0),"f":"Sunday, October 23, 2016"},null,null,null,null,null,true]],"showHeadlines":false,"percentData":false,"colors":["#3f85f2"],"height":230}
如何使用 preg_match 找到它?我试过了
$data = preg_match('/^(var chartData = |; var htmlChart)\/$/', $url2, $output_array);
print_r($output_array);
我使用 first 和 last 关键字来尝试获取它们之间的内容(成为我想要的数据)
我试过使用 phpliveregex,它实际上确实可以获取我想要的东西(找到 var chartData 字符串)http://www.phpliveregex.com/p/hCB 但是当我尝试克隆它时它不起作用。
我的问题是,我如何使用 preg_match() 从 Google 趋势中提取此 json 对象(在 chartData 变量内),因为我尝试过的还没有一直在工作。
问题似乎来自 "JSON string" 中的几个值,这些值看起来都像 new Date(2016, 9, 14, 12, 0)
,包含空格且未包含在引号之间。您可以解决在它们周围添加引号的问题:
$str = preg_replace('~:(new Date\([^)]*\))~', ':""', $str);
$jsonArray = json_decode($str, true);
这应该会有帮助
preg_match('/chartData\s+=\s+\{(.+)\}/', $url2, $output_array);
$charData = $output_array[0];