0

I get the content from an URL: http://money18.on.cc/js/real/hk/quote/16981_r.js

The content of this file is something like this:

M18.r_16981 = {
    "ltt": '2018/06/21 11:43',
    "np": '0.050',
    "iep": '0.000',
    "iev": '0',
    "ltp": '0.050',
    "vol": '940000',
    "tvr": '49860',



    "dyh": '0.060',
    "dyl": '0.049'
};

I want to extract the number after "vol", i.e. 940000.

And this is my php:

<?php

$content = file_get_contents("http://money18.on.cc/js/real/hk/quote/16981_r.js");

echo $content."<br>";

preg_match("/(?<=vol\": ').*(?=', \"tvr)/", $content, $output_array);

print_r(array_values($output_array));
?>

The result returns nothing. Is the Regex wrong or any other problem?

Thanks.

  • what kind of strange content format is this? is this meant to be JSON? do you control the content? – steros Jun 21 '18 at 06:19
  • Are there more complex scenarios that you're trying to handle? Seems that `\"vol\":\s*'(\d+)'` should work just fine for what you've described. – Greg Schmidt Jun 21 '18 at 06:19

2 Answers2

1

In your positive lookahead (?= you could replace the whitespace with \s* to match zero or more (or \s+ to match one or more) whitespace characters.

(?<=vol": ').*(?=',\s*"tvr)

Regex demo

Php demo

The fourth bird
  • 154,723
  • 16
  • 55
  • 70
0

Bit of a long shot but...

If that content is actually meant to represent JSON and you have control over it: get it into proper format as it is much easier and secure to get the data you want out of it then.

$jsonString = <<<JSON
{
    "ltt": "2018/06/21 11:43",
    "np": "0.050",
    "iep": "0.000",
    "iev": "0",
    "ltp": "0.050",
    "vol": "940000",
    "tvr": "49860",
    "dyh": "0.060",
    "dyl": "0.049"
}
JSON;

$jsonAsArray = json_decode($jsonString, true);
var_dump($jsonAsArray['vol']);
steros
  • 1,794
  • 2
  • 26
  • 60