从以下php字符串中提取数据数组(5436,342w5 ...)内容的最佳方法是什么:
series: [{
type: 'area',
name: 'product_data',
data: [ 5436, 342w5, 564s5, 6778, 8954, 567e5, 6578, 67584 ]
}]
以下是什么?
preg_match("/(.*), (.*)/", $input_line, $output_array);
提前致谢:)
编辑:下面的完整代码
PHP:
$file = "file.html";
$file = file_get_contents($file);
$input_line = strip_tags($file);
$data = preg_match("/(.*), (.*)/", $input_line, $output_array);
print_r($output_array);
file.html:
<!DOCTYPE>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Index</title>
<script type="text/javascript">
var chart;
jQuery(document).ready(function() {
chart = new Highcharts.Chart({
series: [{
type: 'area',
name: 'product_data',
data: [ 5436, 342w5, 564s5, 6778, 8954, 567e5, 6578, 67584 ]
}]
});
});
</script>
<body>
</body>
</head>
答案 0 :(得分:0)
你可以用这种模式做到这一点,这不是更简单的方法,但它是一次完成并且是有效的:
$pattern = "~
(?:
\G(?!\A), # anchored to the previous match
|
\A # anchored at the start of the string
[^s]*(?:s(?!eries:)[^s]*)* # all until 'series:'
series:
[^'d]*(?:'[^']*'[^'d]*|\Bd|d(?!ata:))* # all until 'data:'
data:
\s*\[
)
\s*\K # remove all on the left from the result
[^], ]+ # match the item
~x";
if (preg_match_all($pattern, $str, $m))
print_r($m[0]);
您可以使用更传统的方法,首先提取数组,然后将其分解为项目:
$pattern = '~\bdata: \[\K[^]]+~';
if (preg_match($pattern, $str, $m)) {
$items = explode(', ', trim($m[0]));
print_r($items);
}
答案 1 :(得分:0)
或者就是这样:
preg_match('#data:\s\[(.*)\]#', $input_line, $output_array);
$data = explode(',', $output_array[1]);
print_r($data);
答案 2 :(得分:0)
不完全是你正在寻找的答案,但是这里......
数据是JSON编码的,但是要让PHP理解它,需要进行一些调整:
$:~/testscripts> cat js.php
<?php
// Raw data
$input_line=<<<STRING1
series: [{
type: 'area',
name: 'product_data',
data: [ 5436, 342w5, 564s5, 6778, 8954, 567e5, 6578, 67584 ]
}]
STRING1;
$data=json_decode($input_line,true);
echo "Decoded: ".print_r($data,true).PHP_EOL;
echo "---------------------".PHP_EOL;
// Adjust format
$d2=<<<STRING2
{"series": [{
"type":"area",
"name": "product_data",
"data": [ "5436", "342w5", "564s5", "6778", "8954", "567e5", "6578", "67584" ]
}]
}
STRING2;
$dd2=json_decode($d2,true);
echo "Decoded D2: ".print_r($dd2,true).PHP_EOL;
echo "---------------------".PHP_EOL;
// Reverse
$a=array(
'series'=>array(
array(
'type'=>'area',
'name'=>'product_data',
'data'=>array('5436','342w5','564s5', '6778', '8954', '567e5', '6578', '67584')
)
)
);
echo "Encoded: ".json_encode($a).PHP_EOL;
$series=$a['series'];
$dummy=reset($series); // return the first element of array
$data=$dummy['data'];
echo "Data: ".print_r($data,true).PHP_EOL;
而且,当你运行上述内容时,你会得到:
$~/testScripts> php -f js.php
Decoded:
---------------------
Decoded D2: Array
(
[series] => Array
(
[0] => Array
(
[type] => area
[name] => product_data
[data] => Array
(
[0] => 5436
[1] => 342w5
[2] => 564s5
[3] => 6778
[4] => 8954
[5] => 567e5
[6] => 6578
[7] => 67584
)
)
)
)
---------------------
Encoded: {"series":[{"type":"area","name":"product_data","data":["5436","342w5","564s5","6778","8954","567e5","6578","67584"]}]}
Data: Array
(
[0] => 5436
[1] => 342w5
[2] => 564s5
[3] => 6778
[4] => 8954
[5] => 567e5
[6] => 6578
[7] => 67584
)
可以看出,即使$input_line
和执行json_encode
的结果相似,但只有经过调整的原始数据才能正常运行。
您需要通过将所有内容括在语音标记($input_line
而不是"
)中来调整'
,并在开头和结尾添加大括号(可以使用更简单的方法)正则表达式集合然后通过json_decode
函数传递 (true
返回一个数组)。之后,它应该是访问数组元素的简单案例
抱歉代码墙不是您想要的。只是想我会添加另一个可能更容易的选项。或许不是