0

OK, so there is a page I'm querying on another server that returns a comma separated list of two values. Something it would return would be:

850,640

I have some PHP code that calls file_get_contents on that page and needs to do some numeric calculations based on the two values.

No matter what I try, I can't seem to get an int value out of this.

$res = trim(file_get_contents('http://thatURL/'));
echo "X" . $res . "X<br/>";
list($x,$y) = array_map(create_function('$a', 'return (int)$a;'), explode(',', $res));

echo "X:$x";

results in the output:

X 850,640 X
X:0

Note the spaces before and after the comma separated values(how the hell? I trim'd them!) and that $x is assigned the value 0.

What am I doing wrong here?

3
  • Have you tried trimming within the custom callback?
    – BoltClock
    Commented Nov 2, 2010 at 1:25
  • I tried your code with $res = trim(" 850,640 "); and it works correctly. Are you sure your URL really contains those numbers? Also note that instead of using your own function, you can use intval() Commented Nov 2, 2010 at 1:26
  • View Source or printing in plaintext will be immensely useful here. As Hamish points out you may be printing HTML surrounding the whitespace, and as far as I can see you're definitely copying and pasting from the browser's viewport and not the HTML source.
    – BoltClock
    Commented Nov 2, 2010 at 1:31

4 Answers 4

3

What am I doing wrong here?

Nothing, as far as I can see, which indicates that the content of $res is not quite what you expect. Could you change the first echo to:

echo htmlentities($res);

My guess is $res contains some un-printed characters, for example, it is actually:

<span> </span>850,640<span> </span>

or

&nbsp;850,640&nbsp;
1
  • You are correct! It had html and body tags in it. Oops. Thanks! Commented Nov 2, 2010 at 1:35
1

Try the following. The array_map and llamda function are arguably overkill for your usage.

$res = " 850,640 ";
echo "X" . $res . "X<br/>";
list($x,$y) = explode(',', trim($res));

echo "X:" . (int)$x;
echo "Y:" . (int)$y;

Worked for me, but I'm not using file_get_contents(). If that doesn't work, something else is being output by the page.

2
  • But when I manually set $res to " 850,640 " like you did, it works... it's making me think there's some other non-whitespace character preceding the 850? Is there any way to see? Commented Nov 2, 2010 at 1:31
  • There is something else returning from that page. Run it thru htmlspecialchars() and see what it spits out. Commented Nov 2, 2010 at 1:39
1

PHP is not a typed language. Use intval to convert a string to integer.

Correction: it is a loosely typed language! That's what I meant!

1
0

Since I was using file_get_contents() on a URL, there was some HTML being put in as well that I didn't notice in my echo because it parsed out... just empty body and html tags. Oops!

1
  • You un-accepted my answer, even thought that's what help you find the cause? :(
    – Hamish
    Commented Oct 19, 2012 at 2:01

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.