user1952084
user1952084

Reputation: 67

get numbers with regex in php

I am trying to extract all the numbers from a page. The page looks like this:

....lots of html code ....
<script>
..some code...
["listidname",[],{"list":["123456","96326478664","12345678901234"]},12]
...more code....
</script>
...even more code...

The amount of numbers in the list can vary, also the 12 at the end is just a random number, so this can vary as well.

what I am trying to do is extract the 123456, 96326478664 and 12345678901234. However I am not really strong with php let alone regexes..

preg_match_all("/(\d+)/", $input, $output);

gives me the numbers, but also all the other numbers on the page...

Can anyone help me with this? Thank you.

Upvotes: 0

Views: 52

Answers (2)

Casimir et Hippolyte
Casimir et Hippolyte

Reputation: 89649

You must first extract the line and after find the numbers:

if (preg_match('~\["listidname",\[],\{"list":(?:[[,]"\d++")++]},\d++]~', $html, $match)) {
    preg_match_all('~"\K\d++~', $match[0] ,$result);
    print_r($result);
}

Upvotes: 0

vaichidrewar
vaichidrewar

Reputation: 9621

If numbers will be in double quotes try

preg_match_all("/\"(\d+)\"/", $input, $output);

Upvotes: 1

Related Questions