Welcome Guest ( Log In | Register )



 
Reply to this topicStart new topic
> Php String To Int Typecasting
Arbitrary
post Mar 25 2007, 06:02 PM
Post #1


Premium Member
Group Icon

Group: [HOSTED]
Posts: 377
Joined: 17-June 06
From: Adblock life
Member No.: 13,992



I've been working on writing this program that needs strings to be converted to integers. Sometimes PHP works by doing the converting for me, but sometimes it just breaks down and quits working. I've got no idea why.

$sub1 = 0+substr($text, 0, 3);
$sub2 = 0+substr($text, 3, 6);
$sub3 = 0+substr($text, 6, 9);
$sum = 0+$sub1+$sub2+$sub3;
echo $sum;

Basically, $text is a string of numbers that looks something like: 296294255268313 and so on.

Then I use substr to get the first three digits/letters of $text (296) and then the second three digits/letters of $text(294) and the third (255). I add 0 to each of these strings so that PHP can automatically cast it into an integer. This works fine.

However, when I then go to add these integers together and put it inside variable $sum, everything goes awry. It outputs a weird (and very large) number. So then I decided to test if it was possible to add integers to strings with the following code:

$sub1 = 294+255+substr($text, 0, 3);
echo $sub1;

And this, to my surprise, outputted the correct sum (845). Does anyone know how this happened or how it can be fixed? Thanks. =]
Go to the top of the page
 
+Quote Post
bluefish
post Mar 25 2007, 07:34 PM
Post #2


Member [ Level 2 ]
Group Icon

Group: Members
Posts: 71
Joined: 16-December 06
Member No.: 18,419



It actually has nothing to do with the casting. You see, you are using substr as the following:

CODE
string substr(string $str, int $start, int $end);


when the correct syntax is as follows:

CODE
string substr(string $str, int $start, int $length);


So you should use:
CODE
$sub2 = 0+substr($text, 3, 3);
$sub3 = 0+substr($text, 6, 3);

in the appropriate place.
I've come across this problem many times because of differences between programming languages. To check a syntax, just go to www.php.net/substr to get it instantly.
Go to the top of the page
 
+Quote Post
Arbitrary
post Mar 25 2007, 08:35 PM
Post #3


Premium Member
Group Icon

Group: [HOSTED]
Posts: 377
Joined: 17-June 06
From: Adblock life
Member No.: 13,992



Oh gods! So that's why. Thank you! =] No wonder the numbers were turning out to be so big.
Go to the top of the page
 
+Quote Post
TavoxPeru
post Mar 25 2007, 10:58 PM
Post #4


Super Member
Group Icon

Group: [HOSTED]
Posts: 794
Joined: 8-April 06
From: Lima - Peru
Member No.: 12,579
myCENTs:78.07



QUOTE(Arbitrary @ Mar 25 2007, 03:35 PM) *
Oh gods! So that's why. Thank you! =] No wonder the numbers were turning out to be so big.

You got that big number because you are not adding your substrings, instead of this what you are doing is to concatenate these substrings. The correct way was posted by bluefish.

Best regards,
Go to the top of the page
 
+Quote Post
iGuest
post Oct 1 2007, 01:28 PM
Post #5


Newbie [ Level 1 ]
Group Icon

Group: Members
Posts: 0
Joined: 1-November 07
Member No.: 25,869



So easy, typecast with (int) on the string.

example: it will typecast string "25" as int

<?

if (is_int((int)"25"))
{
echo "hi";

}
?>

-salahuddin66
Go to the top of the page
 
+Quote Post
iGuest
post Oct 1 2007, 01:35 PM
Post #6


Newbie [ Level 1 ]
Group Icon

Group: Members
Posts: 0
Joined: 1-November 07
Member No.: 25,869



More better way

add 0 with a string which has int valu. (typecasting may create problem in some situation)

$val = "25";

$val = $val +0;

if(is_int($val))
{

echo "hi";
}

-salahuddin66
Go to the top of the page
 
+Quote Post
iGuest
post Sep 9 2008, 07:08 AM
Post #7


Newbie [ Level 1 ]
Group Icon

Group: Members
Posts: 0
Joined: 1-November 07
Member No.: 25,869



Here\\
Php String To Int Typecasting

I am fed of finding logic for this, I even have my own logic, but not working.

The problem: there is a string - $given (it is holding a a string input by user, say "I am 25 or 26 year old boy and not 25.5 and 26.5")

I want to find the integer and float values from the string and display to the user, a sorted list of values by data type.

Like this way:

Integer 25
Integer 26
Float 25.5
Float 26.5
String "I"
String "want"
String "to"
:
:
String "and"


Pleeeeeeeeeeeeeeeeeeeeeeeease any Ideas...
I am fed up of logic.

Mine was :
$array = explode(' ', $given)
And for each $array[$I] element, look for every letter and see its ascii code is in 48 to 57, if yes then settype($array[$I],"integer")
But...

Help HELP

Go to the top of the page
 
+Quote Post

Reply to this topicStart new topic

Collapse

> Similar Topics

Topics Topics
  1. PHP And Texts: Need Help With PHP String Functions(5)
  2. VBScript: Help, Looping Through Registry Keys(1)
  3. How To Remove Query String Using Regular Expressions(4)
  4. Help Spliting A String Into 3 Variables In C++(2)
  5. Sorting A String Vector(0)
  6. Php Any Variable In String.(1)
  7. Check For Occurence Of Substring In String(3)
  8. Strange Ascii Code 22 Character Detected In Connection String(9)
  9. String Theory(9)
  10. String Library Functions(4)


 



- Lo-Fi Version Time is now: 22nd November 2008 - 02:27 PM