Why doesn't a PHP regex return an array?

281

My code is supposed to return an array containing a weather summary, but does not.

How can I make this work?

This is part of a course I'm taking onUdemy.com; my code exactly matches my instructor's, but doesn't return the same results.

I didn't find any syntax errors so there is something else that I'm either missing or nescient of..

$contents=file_get_contents("http://www.weather-forecast.com/locations/San-Francisco/forecasts/latest");

preg_match('3 Day Weather Forecast Summary:<\/b><span class="phrase">(.*?)</s', $contents, $matches);

print_r($matches);
986

Answer

Solution:

You have to add some delimeter around your regex to seperate it from (possible) modifiers. You could use any char you want - ie/ - but as you use this chars inside your regex it would maybe be easier to use something different like the tilde~

$contents=file_get_contents("http://www.weather-forecast.com/locations/San-Francisco/forecasts/latest");

preg_match('~3 Day Weather Forecast Summary:<\/b><span class="phrase">(.*?)</s~', $contents, $matches);

print_r($matches);

Just tested your regex and it seems, the html of the page doesn't match your expression. I slightly modified it as follows:

3 Day Weather Forecast Summary:<\/b>.*?<span class="phrase">([^<]+)

People are also looking for solutions to the problem: Google Analytics API Error with PHP

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.