3v4l.org

run code in 300+ PHP versions simultaneously
<?php // Weather Scraper // Fetch the page for the city $contents = file_get_contents('http://www.weather-forecast.com/locations/Bend/forecasts/latest'); // Extract the 3 day Weather // The following pattern will yeild forecast in $matches[1] as of September 10, 2015 in the U.S.A. // However the multiple <span> blocks may possibly change causing this logic to fail in the future. $pattern = '/3 Day Weather Forecast Summary:<\/b><span class="read-more-small"><span class="read-more-content"> <span class="phrase">(.*?)</s'; preg_match( $pattern, $contents, $matches); $forecast1 = $matches[1]; // Instead, let's take a hopefully more robust approach and extract the forecast from $matches[0] using a less <span> intensive pattern // Note 1: We separately keep track of the search prefix, so we can remove this from the beginning of $matches[0]. // Note 2: We end the search on pattern not with '<', but with '<\/span' because we need to make sure we search to the first closing </span> $patternPrefix = '3 Day Weather Forecast Summary:'; $pattern = '/' . $patternPrefix . '(.*?)<\/span/s'; preg_match( $pattern, $contents, $matches); // $matches[0] starts with the $patternPrefix, so remove that from the beginning. $forecast2 = trim( substr($matches[0], strlen($patternPrefix)) ); echo $forecast1; echo $forecast2;

preferences:
39.19 ms | 402 KiB | 5 Q