doudansui6650 2018-03-21 08:47
浏览 377
已采纳

在PHP中获取IP地址的最后两部分

I'd like to get the two lasts octets from an IP address with PHP. I tried this :

substr(strrchr($ip,'.'),2);

But it seems I misunderstood the code, since it's not working... (eg. if my IP is 192.168.0.79, this line returns only "9"...)

  • 写回答

3条回答 默认 最新

  • doulaobi7988 2018-03-21 08:50
    关注

    you can explode the ip string and get the parts from it.

        $ip = "192.168.0.79";
        $split = explode(".", $ip);
        $part1 = $split[2]; // 0
        $part2 = $split[3];// 79
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?