Jump to content

[SOLVED] converting string to integer resule in 0


bhavin12300

Recommended Posts

hi,i am creating one scraping script.

after scraping ,,y result comes in one arry with all element  numbers(in arry this number are of type string) .

 

i want to add all this element.

so when i convert all element one by one from string to integer, my first element result to 0 . other elements get converted well.

 

dont know where the problem.

can anyone let me know why this happening.

 

                          $t= trim($pieces[0]);
			   $s=intval($t);
			   echo $s;

value of $pieces[0]="3"

above code give me answer 0 rather than 3.

but if i try to convert second element , i mean $pieces[1] it successfully get converted.

 

intval is the wrong function to use

 

Returns the integer value of var , using the specified base  for the conversion (the default is base 10).

 

use this insetad;

 

<?php
$t= trim($pieces[0]);
$s=(int)$t;
echo $s;

sir,

still result is 0.

with your code as well

intval is the wrong function to use

 

Returns the integer value of var , using the specified base  for the conversion (the default is base 10).

 

use this insetad;

 

<?php
$t= trim($pieces[0]);
$s=(int)$t;
echo $s;

 

(int)$string and intval($string) will always result in the same.

OK,

 

do a var_dump at each step so we can see what's going on and report back...

 

<?php
var_dump($pieces[0]);
echo "<br />";

$t = trim($pieces[0]);
var_dump($t);
echo "<br />";

$s = (int) $t;
var_dump($s);

 

here is output of above.

string(5) "3"

string(5) "3"

int(0)

 

Is there any html around the number 3. The string is 5 characters long, but only showing the number 3. Perhaps something like;

 

"<br>3"

 

If this may be the case try stripping the html with strip_tags;

 

<?php
$t = trim($pieces[0]);
$t = strip_tags($t);
$s = (int) $t;
echo $s;

 

If this isn't the case there may be a line ffed in there, something like

 

"\r\n3"

Is there any html around the number 3. The string is 5 characters long, but only showing the number 3. Perhaps something like;

 

"<br>3"

 

If this may be the case try stripping the html with strip_tags;

 

<?php
$t = trim($pieces[0]);
$t = strip_tags($t);
$s = (int) $t;
echo $s;

 

If this isn't the case there may be a line ffed in there, something like

 

"\r\n3"

oh wow sir, it worked,

but really when  i see html source there is nothing ahead and behind "3". may be something invisible on browser???

It will be in your database, prob a linefeed.

 

Try printing $t in a textarea;

 

<?php
echo "<textarea>$t</textarea>";

 

It will prob show on the second line of the textarea instead of the first.

 

Also please take note of Daniel's post. I messed up intval() would've been working just the same as the type casting I put in place

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.