[ create a new paste ] login | about

Link: http://codepad.org/OnRd6g9u    [ raw code | output | fork ]

PHP, pasted on Oct 5:
<?php

function s2int($pinned_id) { 
$num = preg_replace('/^0+(\d+)/', '\\1', $pinned_id);
if (false === ($x = filter_var($num, FILTER_VALIDATE_INT))) {
    echo "Could not convert $pinned_id to an integer";
    return false;
}
return $pinned_id;
}


echo s2int("000010")."\n";
echo s2int(10.00001)."\n";
echo s2int(10)."\n";
echo s2int("10")."\n";
echo s2int("0")."\n";
echo s2int("a")."\n";
echo s2int("a10")."\n";
echo s2int("10a")."\n";
echo s2int("0x1A")."\n";
echo s2int("-100")."\n";


Output:
1
2
3
4
5
6
7
8
9
10
000010
Could not convert 10.00001 to an integer
10
10
0
Could not convert a to an integer
Could not convert a10 to an integer
Could not convert 10a to an integer
Could not convert 0x1A to an integer
-100


Create a new paste based on this one


Comments: