用PHP扩展给定的IP范围 [英] Exploding given IP range with the PHP

查看:64
本文介绍了用PHP扩展给定的IP范围的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个文本区域:

<form action="index.php" method="post">
<textarea name="test" rows="20" cols="20"></textarea>
<input type="submit" />
</form>

我想输入195.2.2.13/16,PHP应该给我这样的列表:

i want to type 195.2.2.13/16 and PHP should give me a list like that:

195.2.2.13
195.2.2.14
195.2.2.15
195.2.2.16

我该如何使用PHP?

推荐答案

我注意到最初发布的代码对于小数点后两位是可以的,但是如果您需要使用3,则下面的代码就可以正常工作.

I noticed that the code posted originally was fine for two decimal points but just incase you need to use 3 the below should work fine.

$input = "195.2.2.13/100";

function ipRange( $input ) {
    $input = explode( "/", $input );
        $numerator = substr( strrchr( $input[0], "." ), 1,  3 );
        $denominator = $input[1];
            $num = strlen( $numerator );
        $range = substr( $input[0], 0, -$num );

    while ( $numerator <= $denominator ) {
        echo $range.$numerator."<br />\n\r";
        $numerator++;
    }
}

// Call function
ipRange($input);

这篇关于用PHP扩展给定的IP范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆