Use PHP regular expressions to determine IP addresses

Source: Internet
Author: User
When learning PHP, you may encounter a PHP regular expression judgment problem. here we will introduce the solution to the PHP regular expression problem. here we will share it with you. In some query websites and search engines, it is often used to determine whether a string is a standard IP address. For example, in Baidu search for a... "> <LINKhref =" http://www.php100.com//statics/style/h

When learning PHP, you may encounter a PHP regular expression judgment problem. here we will introduce the solution to the PHP regular expression problem. here we will share it with you. In some query websites and search engines, it is often used to determine whether a string is a standard IP address.

For example, if you search for an IP address in Baidu, the information of the IP address is displayed, but the IP address that does not comply with the standard is not displayed. In this case, it is necessary to determine whether the string is a standard IP address. The most common method is to use a PHP regular expression. It is too simple to search for some regular expressions on the Internet to judge the IP address. some of them only judge that the IP field is not greater than 255. In fact, there are still a lot to be judged. for example, they can only be numbers and cannot be negative numbers, ". "Only 4 segments are separated. I 'd better organize a function that feels perfect. the code is as follows:

 


  1. Functionis_ip ($ gonten ){
  2. $ Ip = explode (".", $ gonten );
  3. For ($ I = 0; $ I
  4. {  
  5. If ($ ip [$ I]> 255 ){
  6. Return (0 );
  7. }  
  8. }  
  9. Returnereg ("^ [0-9] {1, 3 }\. [0-9] {1, 3 }\. [0-9] {1, 3 }\. [0-9] {1, 3} $ ", $ gonten );
  10. } 

$ Gonten is the string to be queried. you can use is_ip ($ gonten) to determine the value. The return values are FALSE and TRUE.

1. the eregi statement is case insensitive.


  1. If (eregi ("^ [a-zA-Z0-9] {8, 10} $", "999999999") echo "is eight to ten digits and letters ";

2. replace the ereg_replace of the text in case-sensitive mode


  1. $ String = "Thisisatest ";
  2. // Replace is with was
  3. Echoereg_replace ("is", "was", $ string );

You can also use \ to reverse reference the character 0 in parentheses to indicate that all 1 represents the first matched string, and so on, a maximum of 9! The usage of eregi_replace in case-insensitive text is the same as that in the manual.

3. case-sensitive string partitioning into split statements of different elements


  1. Print_r (split ("[\ n \ t]", "I love \ n Beijing \ t Tiananmen "));

Case insensitive: the spliti statement is used the same as above.

4. only the case-sensitive regular expression SQL _regcase is supported.


  1. $ Name = "xuandun ";
  2. Echosql_regcase ($ name );

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.