bhavin12300 Posted May 26, 2009 Share Posted May 26, 2009 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. Quote Link to comment Share on other sites More sharing options...
gevans Posted May 26, 2009 Share Posted May 26, 2009 code? Quote Link to comment Share on other sites More sharing options...
bhavin12300 Posted May 26, 2009 Author Share Posted May 26, 2009 $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. Quote Link to comment Share on other sites More sharing options...
gevans Posted May 26, 2009 Share Posted May 26, 2009 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; Quote Link to comment Share on other sites More sharing options...
bhavin12300 Posted May 26, 2009 Author Share Posted May 26, 2009 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 Quote Link to comment Share on other sites More sharing options...
gevans Posted May 26, 2009 Share Posted May 26, 2009 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); Quote Link to comment Share on other sites More sharing options...
Daniel0 Posted May 26, 2009 Share Posted May 26, 2009 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. Quote Link to comment Share on other sites More sharing options...
bhavin12300 Posted May 26, 2009 Author Share Posted May 26, 2009 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) Quote Link to comment Share on other sites More sharing options...
gevans Posted May 26, 2009 Share Posted May 26, 2009 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" Quote Link to comment Share on other sites More sharing options...
bhavin12300 Posted May 26, 2009 Author Share Posted May 26, 2009 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??? Quote Link to comment Share on other sites More sharing options...
gevans Posted May 26, 2009 Share Posted May 26, 2009 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 Quote Link to comment Share on other sites More sharing options...
bhavin12300 Posted May 26, 2009 Author Share Posted May 26, 2009 ok. i will check this as well. again thanks. awesome. topic solved Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.