Jump to content

string to int - updated with more info


simbawoomba

Recommended Posts

hi

i am tryint to convert a string to an integer, i have the following code

function strtoint($str) {
    $ip = 0;
    $tetr = explode(".", $str);
    for ($i = 0; $i < 4; $i++) {
        $ip = $ip << 8;
        $ip += $tetr[$i]; 
    };
    return $ip;
}


<select  size="10" id="templateList" name="templateList" onchange="<?php $a = strtoint('templateList.value');

    echo 'displayTemplate('.$templateArray[$a][1].',MessageTemplate)';

?>

">;

however $a is always equal to 0.
templateList.value can be equal to "0" or "1" (up to any value) ect however it always comes out as 0 can someone please help

thanks in advance
Link to comment
https://forums.phpfreaks.com/topic/32825-string-to-int-updated-with-more-info/
Share on other sites

I think your function strtoint should be iptoint.
In case you wanted to convert an ip4 to an integer, there exist a function 'ip2long' of PHP to do that for you.

echo ip2long('192.168.1.1'); // -1062731519

this echo a negative number because i'm using window. in Unix server, it will be correct

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.