-1

I am using file_get_contents to scrape some data from a web page. This part is working fine. I just need 1 value on the page. My regex is not great so it will be helpful if you can help.

This is the data I want to retrieve

var number = 100;

I only need to get the number. The number is always going to be different but var number will always be the same.

 $regex = '/var number =/';
 preg_match($regex,$data,$match); 

The above will return var number =, how can I make it return the actual number?

bouteillebleu
  • 2,456
  • 23
  • 32
user742736
  • 2,629
  • 5
  • 30
  • 40
  • 2
    Use a placeholder, like `(.*)` or `\d`... * See also [Open source RegexBuddy alternatives](http://stackoverflow.com/questions/89718/is-there) and [Online regex testing](http://stackoverflow.com/questions/32282/regex-testing) for some helpful tools, or [RegExp.info](http://regular-expressions.info/) for a nicer tutorial. – mario Jun 12 '13 at 01:30
  • @mario: (+1) for open source RegexBuddy alternatives, I didn't know it existed – Casimir et Hippolyte Jun 12 '13 at 01:51

1 Answers1

0

Pretty simple: var\s+number\s*=\s*(\d+)\s*;.

What does this mean ?

  • var : match var
  • \s+ : match white space 1 or more times
  • number : match number
  • \s* : match white space 0 or more times
  • = : match =
  • \s* : match white space 0 or more times
  • (\d+) : group and match a digit 1 or more times
  • \s* : match white space 0 or more times
  • ; : match ;

If you want to match several of these variables you should use preg_match_all.
Online demo

HamZa
  • 14,671
  • 11
  • 54
  • 75