From: klaussilveira
Operating system: UNIX
PHP version: 5.3.8
Package: Filter related
Bug Type: Bug
Bug description:FILTER_SANITIZE_NUMBER_INT fails to filter strings with plus
and minus
Description:
------------
The filter_var FILTER_SANITIZE_NUMBER_INT filter fails to sanitize plus and
minus
signs in a string. This is the expected behavior, since + and - are
accepted in
an integer. However, the filter fails to recognize multiple + and -,
returning an
string instead of an integer.
For example:
filter_var("I'm+captain4", FILTER_SANITIZE_NUMBER_INT; // returns +4, OK!
filter_var("I'm++captain4", FILTER_SANITIZE_NUMBER_INT; // returns ++4,
FAILURE!
I wrote a small patch that makes the filter ignore + and - signs, which, i
believe, it's the best behavior for this.
Test script:
---------------
<?php
// Normal behavior
$a = filter_var("I'm+captainSp4rrow!", FILTER_SANITIZE_NUMBER_INT);
$b = filter_var("I'm+captain4", FILTER_SANITIZE_NUMBER_INT);
echo "$a and $b" . PHP_EOL;
echo $a + $b . PHP_EOL;
// Problems comes in when we have multiple minus or plus signs in the
string
$a = filter_var("I'm++captainSp4rrow!", FILTER_SANITIZE_NUMBER_INT);
$b = filter_var("I'm++captain4", FILTER_SANITIZE_NUMBER_INT);
echo "$a and $b" . PHP_EOL;
echo $a + $b . PHP_EOL;
Expected result:
----------------
4 and 4
8
4 and 4
8
--
Edit bug report at https://bugs.php.net/bug.php?id=60541&edit=1
--
Try a snapshot (PHP 5.4):
https://bugs.php.net/fix.php?id=60541&r=trysnapshot54
Try a snapshot (PHP 5.3):
https://bugs.php.net/fix.php?id=60541&r=trysnapshot53
Try a snapshot (trunk):
https://bugs.php.net/fix.php?id=60541&r=trysnapshottrunk
Fixed in SVN:
https://bugs.php.net/fix.php?id=60541&r=fixed
Fixed in SVN and need be documented:
https://bugs.php.net/fix.php?id=60541&r=needdocs
Fixed in release:
https://bugs.php.net/fix.php?id=60541&r=alreadyfixed
Need backtrace:
https://bugs.php.net/fix.php?id=60541&r=needtrace
Need Reproduce Script:
https://bugs.php.net/fix.php?id=60541&r=needscript
Try newer version:
https://bugs.php.net/fix.php?id=60541&r=oldversion
Not developer issue:
https://bugs.php.net/fix.php?id=60541&r=support
Expected behavior:
https://bugs.php.net/fix.php?id=60541&r=notwrong
Not enough info:
https://bugs.php.net/fix.php?id=60541&r=notenoughinfo
Submitted twice:
https://bugs.php.net/fix.php?id=60541&r=submittedtwice
register_globals:
https://bugs.php.net/fix.php?id=60541&r=globals
PHP 4 support discontinued:
https://bugs.php.net/fix.php?id=60541&r=php4
Daylight Savings: https://bugs.php.net/fix.php?id=60541&r=dst
IIS Stability:
https://bugs.php.net/fix.php?id=60541&r=isapi
Install GNU Sed:
https://bugs.php.net/fix.php?id=60541&r=gnused
Floating point limitations:
https://bugs.php.net/fix.php?id=60541&r=float
No Zend Extensions:
https://bugs.php.net/fix.php?id=60541&r=nozend
MySQL Configuration Error:
https://bugs.php.net/fix.php?id=60541&r=mysqlcfg