简体   繁体   中英

How to extract data from this PHP string using preg_match?

What would be the best way to extract the content within the data array (5436, 342w5...) from the following php string:

series: [{
    type: 'area',
    name: 'product_data',
    data: [ 5436, 342w5, 564s5, 6778, 8954, 567e5, 6578, 67584 ]
}]

Something along the following?

preg_match("/(.*), (.*)/", $input_line, $output_array);

Thanks in advance :)

Edit: complete code below

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>

You can do it with this pattern, it isn't the more simple way but it is done in one shot and is efficient:

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

demo

You can use a more conventional way that consists to extract the array first and after to explode it into items:

$pattern = '~\bdata: \[\K[^]]+~';

if (preg_match($pattern, $str, $m)) {
    $items = explode(', ', trim($m[0]));
    print_r($items);
} 

Or just something like this :

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

Not exactly the answer you're looking for but here goes...

The data is JSON encoded but for PHP to understand it, it needs a bit of adjustment:

$:~/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;

And, when you run the above, you get:

$~/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
)

As can be seen, even though the $input_line and the result of performing json_encode are similar, only the adjusted raw data is working properly.

You'll need to adjust the $input_line by enclosing everything in speech marks ( " and not ' ), and adding curly braces at the start and end (possible using a simpler set of regular expressions) then pass that through a json_decode function (the true returns an array). After that, it should be a simple case of accessing an array element

Sorry for the wall of code which isn't what you want. Just thought I would add another option which may be easier. Or maybe not

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM