Jump to content

Allowing both positive and negative values from query


calivw78

Recommended Posts

Hello all,

    I am currently working with some code that I found online to query the temperature from wunderground.com.  The problem that I am running into, is that the code does not appear to allow negative values.  Actually, I think it is simply not allowing the "-" to appear before the number.  I am not very fluent in php, but am reading plenty of resources.  None of those have helped me out yet though and my messing around with with the "value=" section of the below code has not resulted in the desired results.  Anyone have some thoughts?

 

Here is what I am currently using:

if (preg_match('#pwsvariable="tempf" english="°F" metric="°C" value="([0-9\.]+)">#', $page, $matches)) { $current_temp = $matches[1];

 

Thanks in advance.

Link to comment
Share on other sites

For future reference, I fixed it by adjusting the code in my initial post to use

value="(-?[0-9\.]+)"

This allowed the negative character to be displayed.

 

In the end though, I found an easier way to get the values via XML

#!/usr/bin/php
#query weather data from wunderground xml for use with mrtg / rrdtool graphing utilities
<?php

//Get the XML document loaded into a variable
$ch = curl_init("http://api.wunderground.com/auto/wui/geo/WXCurrentObXML/index.xml?query=80013");

curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$url = curl_exec($ch);
curl_close($ch);

$xml = new SimpleXMLElement($url);
echo $xml->temp_f."\n";
echo $xml->windchill_f."\n";

?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.