2

I have the following website: http://stationmeteo.meteorologic.net/metar/your-metar.php?icao=LFRS&day=070308

I want to extract data from it. I tried using file_get_contents and some regular expressions, but something is not working.

this is the code I tried:

$content=file_get_contents('http://stationmeteo.meteorologic.net/metar/your-metar.php? icao=LFMN&day=010513');

preg_match('/00\:30 07\/03\/2008(.+)01\:30 07\/03\/2008/',$content,$m);
echo $m[0];
echo $m[1];

It's giving me undefined offset 0 and 1. If I copy the content of the web page directly to $content instead of using file_get_contents, it works fine.

What am I missing?

6
  • When your using file_get_contents(...), what are your getting in $content ? Commented May 31, 2013 at 12:04
  • You're getting no matches because there's no timestamps that match 00:00 01/05/2013? Commented May 31, 2013 at 12:05
  • sorry I set the date wrong I mean, 00:30 07/03/2008 and 01:30 07/03/2008 Commented May 31, 2013 at 12:06
  • Is file_get_contents returning something at all? Commented May 31, 2013 at 12:07
  • yes, if I echo $content, it will return the content of the website. Commented May 31, 2013 at 12:09

1 Answer 1

2

The problem is that .+ matches any characters except newlines, and there is a newline character in the text you're trying to match.

Try

preg_match('~00:30 07/03/2008(.+)01:30 07/03/2008~s',$content,$m);

(using ~ as a delimiter so you don't have to escape all those slashes, by the way)

The next question is: Why don't I get this problem when copying the contents of the webpage directly into $content? Well, all whitespace is normalized to a single space when a webpage is rendered, turning the \n that's present in the page's source code (press Ctrl-U to see it) into a simple space. And .+ matches that space.

Sign up to request clarification or add additional context in comments.

1 Comment

Ok, now I understood what was going on. and thx for the ~ delimiter tip. (really helpful)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.