edotom Posted June 4, 2013 Share Posted June 4, 2013 Hi I want to convert ipv4 and Ipv6 addresses to netmask lenght like: 255.255.0.0 result 16 I was thinking in splitting into arrays and find the byte and then just count the number of bits It this possible? thanks in advance Quote Link to comment Share on other sites More sharing options...
requinix Posted June 4, 2013 Share Posted June 4, 2013 strspn(base_convert(bin2hex(inet_pton("255.255.0.0")), 16, 2), "1")I'm sure there's a better solution. Quote Link to comment Share on other sites More sharing options...
edotom Posted June 4, 2013 Author Share Posted June 4, 2013 It works !! but just for ipv4, How can I convert Ipv6 to bin? Quote Link to comment Share on other sites More sharing options...
requinix Posted June 5, 2013 Share Posted June 5, 2013 It should work for IPv6 too. Maybe your PHP doesn't support it? var_dump(defined('AF_INET6')); // true=has support, false=no supportWhat do you get when you try? Would be a warning and a result of 0. Quote Link to comment Share on other sites More sharing options...
edotom Posted June 5, 2013 Author Share Posted June 5, 2013 sadly I receive false, therefore there's no support for 64 Quote Link to comment Share on other sites More sharing options...
requinix Posted June 5, 2013 Share Posted June 5, 2013 Then you'll probably have to make your own function to deal with it. Since the problem is simply finding the number of leading 1 bits I'd just go with a function to do both IPv4 and IPv6 that gives you exactly the result you need. Means you don't have to parse the address for real, simply count how many 1 bits you find until the first 0. "Simply". Thing is that there are a few valid formats you'd have to account for: pieces not having all four hex digits, omitted zeroes with a ::, a trailing IPv4 address... Unless you can restrict the input. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.