簡體   English   中英

如何使用preg_match從此PHP字符串中提取數據?

[英]How to extract data from this PHP string using preg_match?

從以下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>

您可以使用此模式進行操作,這不是更簡單的方法,但是可以一次完成並且非常有效:

$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);
} 

或者只是這樣:

preg_match('#data:\s\[(.*)\]#', $input_line, $output_array);
$data = explode(',', $output_array[1]);
print_r($data);

不完全是您要尋找的答案,但是這里...

數據是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返回一個數組)。此后,應該是訪問數組元素的簡單情況

抱歉,這不是您想要的代碼。 只是想我會添加另一個可能更簡單的選項。 或者可能不是

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM