Ash Van Wilder
Ash Van Wilder

Reputation: 105

regex to find number between a number and a bracket

I have something like

Page 1 of 7 (1-49 of 325)

I need to find the last page by using regex.

Here is what i have as regex expression

<?php

$page = 'Page 1 of 7 (1-49 of 325)';

$matches = array();
$t = preg_match('/of(.*?)\(/s', $page, $matches);
print_r($matches[1]);


?>

It works fine, it outputs 7.

My problem is when ever i use

<?php

$page = file_get_contents('http://www.exaample.com');

$matches = array();
$t = preg_match('/of(.*?)\(/s', $page, $matches);
print_r($matches[1]);


?>

i get a lot of text, but i dont get the output '7'.

Upvotes: 3

Views: 158

Answers (3)

David Harris
David Harris

Reputation: 2707

<?php
$str = 'Page 1 of 7 (1-49 of 325)';
preg_match('/\([0-9]+\-([0-9]+) of [0-9]+\)/', $str, $matches);
var_dump($matches);

Not tested.

Upvotes: -1

burning_LEGION
burning_LEGION

Reputation: 13450

use this regular expression \d+(?=\s*\()

Upvotes: 0

Tchoupi
Tchoupi

Reputation: 14691

This works:

$t = preg_match('/Page [0-9]+ of ([0-9]+)/i', $page, $matches);

Upvotes: 4

Related Questions