FAQ
Edit report at http://pear.php.net/bugs/bug.php?id=17662&edit=1

ID: 17662
Updated by: hm2k@php.net
Reported By: hm2k at php dot net
Summary: Is there really need for a regex or does a PHP
function do the trick?
Status: Open
Type: Feature/Change Request
Package: Net_CheckIP
Package Version: SVN
PHP Version: Irrelevant
Roadmap Versions:
New Comment:

Patch attached.

I see no reason not to include this improvement.


Previous Comments:
------------------------------------------------------------------------

[2011-02-15 17:46:10] hm2k

Added #patch bug:17662;patch:net-checkip-no-regex;revision:1297791970;.

------------------------------------------------------------------------

[2010-08-09 09:41:56] hm2k

Instead of
if (!is_numeric($oct[$i])) {

Try:
if (!is_int($oct[$i])) {

Obviously is_numeric includes decimals, which is no good here.

------------------------------------------------------------------------

[2010-08-07 14:51:04] doconnor

-Type: Bug
+Type: Feature/Change Request


------------------------------------------------------------------------

[2010-08-05 21:14:20] hm2k

Description:
------------
if (!preg_match("/^[0-9]+$/", $oct[$i])) {

Try:

if (!is_numeric($oct[$i])) {

See: http://talks.php.net/show/php-best-practices/36

Alternatively, this whole script could be replaced by the following
script:

<?php
function checkip ($ip) {
return
preg_match('/^\s*(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\s*$/m',$ip);
}
?>

------------------------------------------------------------------------

Search Discussions

  • Hm2k at Feb 16, 2011 at 1:01 pm
    Edit report at http://pear.php.net/bugs/bug.php?id=17662&edit=1

    ID: 17662
    Updated by: hm2k@php.net
    Reported By: hm2k at php dot net
    Summary: Is there really need for a regex or does a PHP
    function do the trick?
    Status: Open
    Type: Feature/Change Request
    Package: Net_CheckIP
    Package Version: SVN
    PHP Version: Irrelevant
    -Assigned To:
    +Assigned To: mj
    Roadmap Versions:
    New Comment:

    -Assigned To:
    +Assigned To: mj



    Previous Comments:
    ------------------------------------------------------------------------

    [2011-02-15 17:46:42] hm2k

    Patch attached.

    I see no reason not to include this improvement.

    ------------------------------------------------------------------------

    [2011-02-15 17:46:10] hm2k

    Added #patch bug:17662;patch:net-checkip-no-regex;revision:1297791970;.

    ------------------------------------------------------------------------

    [2010-08-09 09:41:56] hm2k

    Instead of
    if (!is_numeric($oct[$i])) {

    Try:
    if (!is_int($oct[$i])) {

    Obviously is_numeric includes decimals, which is no good here.

    ------------------------------------------------------------------------

    [2010-08-07 14:51:04] doconnor

    -Type: Bug
    +Type: Feature/Change Request


    ------------------------------------------------------------------------

    [2010-08-05 21:14:20] hm2k

    Description:
    ------------
    if (!preg_match("/^[0-9]+$/", $oct[$i])) {

    Try:

    if (!is_numeric($oct[$i])) {

    See: http://talks.php.net/show/php-best-practices/36

    Alternatively, this whole script could be replaced by the following
    script:

    <?php
    function checkip ($ip) {
    return
    preg_match('/^\s*(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\s*$/m',$ip);
    }
    ?>

    ------------------------------------------------------------------------
  • Mj at Feb 28, 2011 at 7:07 pm
    Edit report at http://pear.php.net/bugs/bug.php?id=17662&edit=1

    ID: 17662
    Updated by: mj@php.net
    Reported By: hm2k at php dot net
    Summary: Is there really need for a regex or does a PHP
    function do the trick?
    -Status: Assigned
    +Status: Wont fix
    Type: Feature/Change Request
    Package: Net_CheckIP
    Package Version: SVN
    PHP Version: Irrelevant
    Assigned To: mj
    Roadmap Versions:
    New Comment:

    -Status: Assigned
    +Status: Wont fix
    Using is_int() is not right here because it only works if the given
    variable is acutally of type int already:

    <?php
    var_dump(is_int("123"));
    var_dump(is_int(123));
    ?>

    will actually print

    bool(false)
    bool(true)

    If we were to change something in the really really ancient code, we
    could completely get rid of the preg_match() check, because the next
    check will work just fine for rejecting any input that is not an integer
    between 0 and 255.


    Previous Comments:
    ------------------------------------------------------------------------

    [2011-02-16 14:03:21] hm2k

    -Assigned To:
    +Assigned To: mj


    ------------------------------------------------------------------------

    [2011-02-15 17:46:42] hm2k

    Patch attached.

    I see no reason not to include this improvement.

    ------------------------------------------------------------------------

    [2011-02-15 17:46:10] hm2k

    Added #patch bug:17662;patch:net-checkip-no-regex;revision:1297791970;.

    ------------------------------------------------------------------------

    [2010-08-09 09:41:56] hm2k

    Instead of
    if (!is_numeric($oct[$i])) {

    Try:
    if (!is_int($oct[$i])) {

    Obviously is_numeric includes decimals, which is no good here.

    ------------------------------------------------------------------------

    [2010-08-07 14:51:04] doconnor

    -Type: Bug
    +Type: Feature/Change Request


    ------------------------------------------------------------------------

    The remainder of the comments for this report are too long. To view
    the rest of the comments, please view the bug report online at
    http://pear.php.net/bugs/bug.php?id=17662
  • Hm2k at Feb 28, 2011 at 8:17 pm
    Edit report at http://pear.php.net/bugs/bug.php?id=17662&edit=1

    ID: 17662
    Updated by: hm2k@php.net
    Reported By: hm2k at php dot net
    Summary: Is there really need for a regex or does a PHP
    function do the trick?
    -Status: Wont fix
    +Status: Assigned
    Type: Feature/Change Request
    Package: Net_CheckIP
    Package Version: SVN
    PHP Version: Irrelevant
    Assigned To: mj
    Roadmap Versions:
    New Comment:

    -Status: Wont fix
    +Status: Assigned
    The idea here is to completely get rid of the preg_match, which isn't
    required.

    - if (!preg_match("/^[0-9]+$/", $oct[$i])) {
    + if (!is_numeric($oct[$i]) || ceil($oct[$i]) != $oct[$i]) {


    Previous Comments:
    ------------------------------------------------------------------------

    [2011-02-28 20:10:22] mj

    -Status: Assigned
    +Status: Wont fix
    Using is_int() is not right here because it only works if the given
    variable is acutally of type int already:

    <?php
    var_dump(is_int("123"));
    var_dump(is_int(123));
    ?>

    will actually print

    bool(false)
    bool(true)

    If we were to change something in the really really ancient code, we
    could completely get rid of the preg_match() check, because the next
    check will work just fine for rejecting any input that is not an integer
    between 0 and 255.

    ------------------------------------------------------------------------

    [2011-02-16 14:03:21] hm2k

    -Assigned To:
    +Assigned To: mj


    ------------------------------------------------------------------------

    [2011-02-15 17:46:42] hm2k

    Patch attached.

    I see no reason not to include this improvement.

    ------------------------------------------------------------------------

    [2011-02-15 17:46:10] hm2k

    Added #patch bug:17662;patch:net-checkip-no-regex;revision:1297791970;.

    ------------------------------------------------------------------------

    [2010-08-09 09:41:56] hm2k

    Instead of
    if (!is_numeric($oct[$i])) {

    Try:
    if (!is_int($oct[$i])) {

    Obviously is_numeric includes decimals, which is no good here.

    ------------------------------------------------------------------------

    The remainder of the comments for this report are too long. To view
    the rest of the comments, please view the bug report online at
    http://pear.php.net/bugs/bug.php?id=17662
  • Mj at Feb 28, 2011 at 8:52 pm
    Edit report at http://pear.php.net/bugs/bug.php?id=17662&edit=1

    ID: 17662
    Updated by: mj@php.net
    Reported By: hm2k at php dot net
    Summary: Is there really need for a regex or does a PHP
    function do the trick?
    -Status: Assigned
    +Status: Wont fix
    Type: Feature/Change Request
    Package: Net_CheckIP
    Package Version: SVN
    PHP Version: Irrelevant
    Assigned To: mj
    Roadmap Versions:
    New Comment:

    -Status: Assigned
    +Status: Wont fix
    James,

    again, what you suggested will not work.

    <?php
    $var = "+127";
    var_dump(!is_numeric($var) || ceil($var) != $var);
    ?>

    will result in

    bool(false)

    Thus with your patch the input "+127.0.0.1" will yield a positive return
    value, which clearly is bogus. Using ctype_digit() will probably work,
    but I'm hesitating to go this route because I don't intend to add an
    extension dependency.

    I suggest you stop investing so much of your time in this old package
    and focus on Net_CheckIP2 instead. Thanks.


    Previous Comments:
    ------------------------------------------------------------------------

    [2011-02-28 21:19:51] hm2k

    -Status: Wont fix
    +Status: Assigned
    The idea here is to completely get rid of the preg_match, which isn't
    required.

    - if (!preg_match("/^[0-9]+$/", $oct[$i])) {
    + if (!is_numeric($oct[$i]) || ceil($oct[$i]) != $oct[$i]) {

    ------------------------------------------------------------------------

    [2011-02-28 20:10:22] mj

    -Status: Assigned
    +Status: Wont fix
    Using is_int() is not right here because it only works if the given
    variable is acutally of type int already:

    <?php
    var_dump(is_int("123"));
    var_dump(is_int(123));
    ?>

    will actually print

    bool(false)
    bool(true)

    If we were to change something in the really really ancient code, we
    could completely get rid of the preg_match() check, because the next
    check will work just fine for rejecting any input that is not an integer
    between 0 and 255.

    ------------------------------------------------------------------------

    [2011-02-16 14:03:21] hm2k

    -Assigned To:
    +Assigned To: mj


    ------------------------------------------------------------------------

    [2011-02-15 17:46:42] hm2k

    Patch attached.

    I see no reason not to include this improvement.

    ------------------------------------------------------------------------

    [2011-02-15 17:46:10] hm2k

    Added #patch bug:17662;patch:net-checkip-no-regex;revision:1297791970;.

    ------------------------------------------------------------------------

    The remainder of the comments for this report are too long. To view
    the rest of the comments, please view the bug report online at
    http://pear.php.net/bugs/bug.php?id=17662

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
grouppear-bugs @
categoriesphp
postedFeb 15, '11 at 4:44p
activeFeb 28, '11 at 8:52p
posts5
users2
websitepear.php.net

2 users in discussion

Hm2k: 3 posts Mj: 2 posts

People

Translate

site design / logo © 2022 Grokbase