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.

 

Link to comment
Share on other sites

                          $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.

 

Link to comment
Share on other sites

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;

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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)

 

Link to comment
Share on other sites

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"

Link to comment
Share on other sites

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???

Link to comment
Share on other sites

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

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.