Page 1 of 1

Variable Won't Act Like Number

Posted: Wed Oct 11, 2006 4:02 pm
by Tomcat7194
Hello. This URL returns numbers: http://gcp.djbradanderson.com/gcpindex.php and I am trying to load the first number using substr(). However, once I load the number, it doesn't act like a numerical variable, so I can't perform calculations with it. Any idea how I can convert it to a number, or if there's a better way to load it so that PHP will know it's numeric? Here's the code I've been using thus far:

Code: Select all

$gcsite = file_get_contents("http://gcp.djbradanderson.com/gcpindex.php");
$gcsite_display = substr($gcsite, 48, 45);
Thanks
Tom

Posted: Wed Oct 11, 2006 4:08 pm
by Luke
substr is made to work with strings (substring) but you can cast to a type by putting the type in parentheses before the assignment...

Code: Select all

$gcsite = (integer) $gcsite;
EDIT: Oh yea and there's settype()

Posted: Wed Oct 11, 2006 4:13 pm
by volka
file_get_contents() fetches the raw source.

Code: Select all

<?php
$gcsite = file_get_contents("http://gcp.djbradanderson.com/gcpindex.php");
$gcsite_display = substr($gcsite, 48, 45);

var_dump($gcsite_display);
?>
prints
string(45) "<seconds><second time='1160600340'>0.4869</se"
and <seconds> is not a numerical value ;)
The whole document looks like
<gcpstats>
<serverTime>1160600306</serverTime>
<seconds><second time='1160600280'>0.9339</second><second time='1160600281'>0.9283</second><second time='1160600282'>0.9291</second><second time='1160600283'>0.9341</second><second time='1160600284'>0.9341</second><second time='1160600285'>0.9344</second><second time='1160600286'>0.9360</second><second time='1160600287'>0.9373</second><second time='1160600288'>0.9394</second><second time='1160600289'>0.9393</second><second time='1160600290'>0.9396</second><second time='1160600291'>0.9412</second><second time='1160600292'>0.9398</second><second time='1160600293'>0.9332</second><second time='1160600294'>0.9319</second><second time='1160600295'>0.9314</second><second time='1160600296'>0.9317</second><second time='1160600297'>0.9317</second><second time='1160600298'>0.9309</second><second time='1160600299'>0.9315</second><second time='1160600300'>0.9316</second><second time='1160600301'>0.9241</second><second time='1160600302'>0.9245</second><second time='1160600303'>0.9226</second><second time='1160600304'>0.9214</second><second time='1160600305'>0.9281</second><second time='1160600306'>0.9281</second><second time='1160600307'>0.9268</second><second time='1160600308'>0.9239</second><second time='1160600309'>0.9240</second><second time='1160600310'>0.9243</second><second time='1160600311'>0.9235</second><second time='1160600312'>0.9177</second><second time='1160600313'>0.9180</second><second time='1160600314'>0.9187</second><second time='1160600315'>0.9170</second><second time='1160600316'>0.9107</second><second time='1160600317'>0.9099</second><second time='1160600318'>0.9052</second><second time='1160600319'>0.9044</second><second time='1160600320'>0.9047</second><second time='1160600321'>0.9044</second><second time='1160600322'>0.9035</second><second time='1160600323'>0.5201</second><second time='1160600324'>0.5204</second><second time='1160600325'>0.5251</second><second time='1160600326'>0.5188</second><second time='1160600327'>0.5158</second><second time='1160600328'>0.5167</second><second time='1160600329'>0.9008</second><second time='1160600330'>0.4799</second><second time='1160600331'>0.4908</second><second time='1160600332'>0.4938</second><second time='1160600333'>0.4917</second><second time='1160600334'>0.4891</second><second time='1160600335'>0.4899</second><second time='1160600336'>0.4927</second><second time='1160600337'>0.4930</second><second time='1160600338'>0.4907</second><second time='1160600339'>0.4942</second></seconds></gcpstats>
Can you use a xml extension for php to parse the document?

Posted: Wed Oct 11, 2006 4:14 pm
by Tomcat7194
The problem though is that the number isn't an integer--when I use settype(), it makes the number equal to 1. Is there a way to preserve the decimals while still converting the substring into a number?
Thanks
Tom

Posted: Wed Oct 11, 2006 4:16 pm
by ambivalent
Is that file supposed to be XML? file_get_contents returns the entire file as a string, probably not want you want?
http://gcp.djbradanderson.com/gcpindex.php wrote:

Code: Select all

<gcpstats>
	<serverTime>1160600068</serverTime>
<seconds><second time='1160600040'>0.9397</second><second time='1160600041'>0.9393</second><second time='1160600042'>0.9368</second><second time='1160600043'>0.9400</second>
[......]
<second time='1160600097'>0.9301</second><second time='1160600098'>0.9313</second><second time='1160600099'>0.9317</second></seconds></gcpstats>

Posted: Wed Oct 11, 2006 4:17 pm
by volka
Tomcat7194 wrote:The problem though is that the number isn't an integer
no, the problem is
substr($gcsite, 48, 45) == "<seconds><second time='1160600340'>0.4869</se"
and that's not a number!

Posted: Wed Oct 11, 2006 4:17 pm
by RobertGonzalez
I think there is a double type (or float type for newer versions), which is what you'd use for decimalized numbers. Have a look at these...

Type Casting
settype()

Posted: Wed Oct 11, 2006 4:17 pm
by Luke
you may want to check out SimpleXML

Posted: Wed Oct 11, 2006 4:20 pm
by Tomcat7194
Yeah, the substring shouldn't be 45 charcters long, but for whatever reason when I run that script it returns only 6 digits--if I change the numbers in substr at all, I get another part of the string. Any reason why this might be happening?

I'm running php 4, so there's not a lot of built in xml support--can someone recommend a good XML extension?

Tom

Posted: Wed Oct 11, 2006 4:21 pm
by volka
try

Code: Select all

<?php
$gcsite = file_get_contents("http://gcp.djbradanderson.com/gcpindex.php"); 

$pattern = '!<second time=\'(\d+)\'>([0-9.]+)</second>!';
preg_match_all($pattern, $gcsite, $matches);

echo $matches[1][0], ' | ', $matches[2][0];
?>

Posted: Wed Oct 11, 2006 4:21 pm
by feyd
Why not use strip_tags() or the "better" version linked from Useful Posts, or some other variant thereof?

Posted: Wed Oct 11, 2006 4:27 pm
by Tomcat7194
I love the the regular expressions idea Volka--it works perfectly, and without the hassle of bringing in an XML parser. Thanks for the suggestion, and thanks to everyone for the fast responses.
Tom