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:
Functionis_ip ($gonten) {
- $ip =explode (".", $gonten);
- for ($i =0; $i <count ($IP); $i + +)
- {
- if ($ip [$i]>255) {
- return (0);
- }
- }
- Returnereg ("^[0-9]{1,3}\.[ 0-9]{1,3}\. [0-9] {1,3}\. [0-9] {1,3}$ ", $gonten);
- }
$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
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
$string = "Thisisatest";
- Replace is being
- 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
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
$name = "Xuandun";
- Echosql_regcase ($name);