Welcome Guest, Not a member yet? Register   Sign In
ctype_digit
#1

[eluser]Seppo[/eluser]
Hey, I´ve founded some differences between CI ctype_digit and PHP...

Code:
<?php
error_reporting(E_ALL);
function ctype_digit2($str)
{
    return ! preg_match('/[^0-9]/', $str);
}
var_dump(ctype_digit(1) == ctype_digit2(1));
var_dump(ctype_digit('1') == ctype_digit2('1'));
var_dump(ctype_digit('1a') == ctype_digit2('1a'));
var_dump(ctype_digit('a1') == ctype_digit2('a1'));
var_dump(ctype_digit('a') == ctype_digit2('a'));
var_dump(ctype_digit('') == ctype_digit2(''));
var_dump(ctype_digit(null) == ctype_digit2(null));
var_dump(ctype_digit(true) == ctype_digit2(true));
var_dump(ctype_digit(array()) == ctype_digit2(array()));
var_dump(ctype_digit(new stdClass()) == ctype_digit2(new stdClass()));
?>

I´m getting

Quote:bool(false)
bool(true)
bool(true)
bool(true)
bool(true)
bool(false)
bool(false)
bool(false)
<br />
<b>Warning</b>: preg_match() expects parameter 2 to be string, array given in <b>document</b> on line <b>5</b><br />
bool(false)
<br />
<b>Warning</b>: preg_match() expects parameter 2 to be string, object given in <b>document</b> on line <b>5</b><br />
bool(false)

I´m not sure if this is actually a problem... any thought? is this worthy to change?

Edit: I´m gettin the same issues with ctype_alnum
#2

[eluser]Derek Jones[/eluser]
ctype_ methods are expecting string input, so will not do quite what one would expect if you give it another type of input. I've always understood it to be good practice to typecast variables you are using ctype_ on if you do not know them to be strings, as if you aren't working with strings, there are other functions available to validate the data. That's just my opinion, though, so I'm open to opposing arguments.

And, for the record, you're fast!
#3

[eluser]Seppo[/eluser]
Fast? you are lucky I was working earlier Tongue

I expect the CI ctype_* do the same thing PHP ones have... maybe you can add a "if (!is_string($str)) return false;" at the begining and you fix most of the issues... however you still have to do something with the empty string, don´t you think so?
#4

[eluser]Derek Jones[/eluser]
I guess my logic was basing it on the fact that everyone that meets the requirements for CI (PHP 4.3.2+) will have these functions available natively, and this is just for people who are on servers who have by choice explicitly disabled them, in which case, I wanted them to function correctly for the spots we are using them. However I don't feel strongly enough about this to resist, and the suggestion to make it more universally correct seems fine, so I shall add:

Code:
if (! is_string($str) OR $str == '')
{
    return FALSE;
}
#5

[eluser]Seppo[/eluser]
Cool, I´ve got all the same results on every example I can think.
Thanks Derek.
#6

[eluser]Derek Jones[/eluser]
No problem, thank you for your feedback.




Theme © iAndrew 2016 - Forum software by © MyBB