IP conversion to number, regular matching IP

Source: Internet
Author: User
Tags explode
storage space Comparison:
VARCHAR type storage 0.0.0.0-255.255.255.255 is 7-15 bytes
int     Type storage is 4 bytes  1 bytes 8 bits  2^31-1  
IPv4 Essence is a 32-bit binary string   can be stored with int 
The IPv4 address (10-in-4) is converted to a 32-bit 2-in-system.
1. Convert to 2 first, then connect
 2. Left 3*8 2*8 1*8 0 Bits 
Conversion:
Conversion Mode PHP built-in Functions Mysql Built-in Functions
string int PHP Long2ip (118.190.3.21) MySQL Inet_ntoa (str_to_int)
int spin String PHP Ip2long (3031005402) MySQL Inet_aton (int_to_str)
function Ipton ($ip) {$ip _arr=explode ('. ', $ip);//Separate IP segment foreach ($ip _arr as $value) {$iphex =dechex ($va Lue)//convert each segment of IP to 16 if (strlen ($iphex) <2)//255 16 is FF, so the 16 feed length of each IP segment will not exceed 2 {$iphex = ' 0 '. $ip hex;//if the converted 16-digit length is less than 2, precede it with a 0//no length of 2, and the first digit is 0 of the 16-in notation, which is to handle the $ipstr when converting the number to IP. = $iphex;//Four segment IP The 16 binary number is connected to get a 16 binary string, the length is 8} return Hexdec ($IPSTR),//Convert the 16 string to 10, get the digital representation of the IP//convert the number to IP, and perform the reverse process of the above function functio
    n Ntoip ($n) {$iphex =dechex ($n);//convert 10 to 16 $len =strlen ($iphex);//Get 16 string length if (strlen ($iphex) <8) {$iphex = ' 0 '. $iphex;//If the length is less than 8, add 0 $len =strlen ($iphex) at the front;//re-get the length of the 16 feed string}//This is because the Ipton function gets the 16 in String, if the first digit is 0, after converting to a number, is not displayed//So, if the length is less than 8, must be the first bit of 0 plus go//Why must be the first 0 it, because in the Ipton function, the next paragraph plus the ' 0 ' in the middle, converted to a number, will not disappear for
    ($i =0, $j =0 $j < $len; $i = $i +1, $j = $j +2) {//loop-intercept 16-in-string, 2-length $ippart =substr ($iphex, $j, 2) per intercept, and the 16-digit number corresponding to each IP segment $fipart =substr ($ippart,0,1)//intercept the first if ($fipart = = ' 0 ') of the 16-digit number {//If the first digit is 0, the original number is only 1 digits $ippart =substr ($ippart, 1, 1);/0

    Intercept the} $ip []=hexdec ($ippart);//convert each paragraph 16 to the corresponding 10 number, that is, the value of each segment of IP} $ip = Array_reverse ($IP);
 Return implode ('. ', $ip);//Connect each segment, returning the original IP value}
Conversion principle:
1. After the string to int     Dechex segment    Max converted to 16 is FF so no more than 2  do length judgment if less than 2 0, spell the 8 in length 16, Hexdec 16 to 10 
. int spin string     inverse procedure
Regular match:
Functionis_ip ($gonten) {  
 $gonten = ' 255.190.3.21 ';

$ip =explode ('. ', $gonten);
For ($i =0 $i <count ($IP); $i + +)
{
    if ($ip [$i]>255) {return
        (0);
    }
}
Preg_match ("/^ (1?\d{1,2}) | ( 2[0-4]\D) | (25[0-5]) \.) {3} ((1?\d{1,2}) | (2[0-4]\d) | (25[0-5]) $/", $gonten);
Var_dump ($gonten);

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.