Use PHP Regular expression to judge IP address skillfully

Source: Internet
Author: User
Tags expression php regular expression query regular expression split

When learning PHP, you may encounter the problem of judging PHP regular expression, here will introduce the solution of the problem of PHP regular expression, here to share 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 an IP address, will display the IP information, and do not meet the standard IP address is not displayed IP information. At this point to determine whether the string is a standard IP, the most common method is to use the PHP regular expression of judgment. Search on the Internet some of the regular IP address, are too simple, and some just judge the IP field is not more than 255, in fact, there are many need to judge, such as only numbers, and can not be negative, but also useful. Split only 4 paragraphs and so on. It's best to organize a function that feels perfect for me, with the following code:

    1. 		Functionis_ip ($gonten) {
    2. $ip =explode (".", $gonten);
    3. for ($i =0; $i <count ($IP); $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 query, it can be judged by IS_IP ($gonten), and returns false and true.

1.eregi Statement case-insensitive judgment

    1. 		if (eregi ("^[a-za-z0-9]{8,10}$", "999999999")) echo "is a number with letters of eight to 10 digits";

2. Ereg_replace to replace text in a case-sensitive way

    1. 		$string = "Thisisatest";
    2. Replace is being
    3. Echoereg_replace (' Is ', ' was ', $string);

You can also use \ to dereference the character 0 in parentheses to indicate that all 1 represents the first matching string and so on up to 9! Specific manuals eregi_replace usage of case-insensitive replacement text Ibid.

3. Case-sensitive way to divide strings into split statements with different elements

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

Case-insensitive partitioning: Spliti statement Usage ditto

4. Adjust to support only case-sensitive regular expression sql_regcase

    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.