Home > Backend Development > PHP Tutorial > How to Use PHP Regular Expressions to Determine IP Address_PHP Tutorial

How to Use PHP Regular Expressions to Determine IP Address_PHP Tutorial

WBOY
Release: 2016-07-15 13:27:36
Original
1340 people have browsed it

When learning PHP, you may encounter the problem of judging PHP regular expressions. Here we will introduce the solutions to PHP regular expression problems and share them with you here. In some query websites and search engines, it is often used to determine whether a certain string is a standard IP address.

For example, if you search for an IP address in Baidu, the IP information will be displayed. IP information that does not meet the standard will not be displayed. At this time, it is necessary to determine whether the string is a standard IP. The most commonly used method is to use PHP regular expressions to determine. I searched some regular rules for judging IP addresses on the Internet, but they are all too simple. Some only judge that the IP field is not larger than 255. In fact, there are many things that need to be judged. For example, it can only be a number and cannot be a negative number. There is also the use of ". "Divide into only 4 segments and so on. It’s best that I compiled a function that I feel is perfect. The code is as follows:
<ol class="dp-xml">
<li class="alt"><span><span>functionis_ip($gonten){  </span></span></li>
<li class="">
<span>$</span><span class="attribute"><font color="#ff0000">ip</font></span><span>=</span><span class="attribute-value"><font color="#0000ff">explode</font></span><span>(”.”,$gonten);  </span>
</li>
<li class="alt">
<span>for($</span><span class="attribute"><font color="#ff0000">i</font></span><span>=</span><span class="attribute-value"><font color="#0000ff">0</font></span><span>;$i</span><strong><font color="#006699"><span class="tag"><</SPAN><SPAN class=tag-name>count</SPAN></FONT></STRONG><SPAN>($ip);$i++)  </SPAN></SPAN><LI class=""><SPAN>{  </SPAN><LI class=alt><SPAN>if($ip[$i]</SPAN><SPAN class=tag><STRONG><FONT color=#006699>></span></font></strong><span>255){  </span>
</li>
<li class=""><span>return(0);  </span></li>
<li class="alt"><span>}  </span></li>
<li class=""><span>}  </span></li>
<li class="alt"><span>returnereg(”^[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}$”,$gonten);  </span></li>
<li class=""><span>} </span></li>
</ol>
Copy after login

$gonten is the string to be queried. You can use is_ip($gonten) to judge it and return FALSE and TRUE.

1. The eregi statement is case-insensitive

<ol class="dp-xml"><li class="alt"><span><span>if(eregi("^[a-zA-Z0-9]{8,10}$","999999999"))echo"是八到十位的数字与字母"; </span></span></li></ol>
Copy after login

2. ereg_replace to replace text in a case-sensitive manner

<ol class="dp-xml">
<li class="alt"><span><span>$</span><span class="attribute"><font color="#ff0000">string</font></span><span>=</span><span class="attribute-value"><font color="#0000ff">"Thisisatest"</font></span><span>;  </span></span></li>
<li class=""><span>//替换is为was  </span></li>
<li class="alt"><span>echoereg_replace("is","was",$string); </span></li>
</ol>
Copy after login

can also be used to backquote the characters in parentheses. 0 means all 1 means the first matching string, and so on up to 9! The usage of eregi_replace in the specific manual is case-insensitive and the replacement text is the same as above

3. Divide the string into different elements in a case-sensitive way. The split statement

<ol class="dp-xml"><li class="alt"><span><span>print_r(split("[nt]","我爱n北京t天安门")); </span></span></li></ol>
Copy after login

is not differentiated. Case division: The usage of the spliti statement is the same as above

4. Adjust the regular expression sql_regcase that only supports case sensitivity

<ol class="dp-xml">
<li class="alt"><span><span>$</span><span class="attribute">name</span><span>=</span><span class="attribute-value">"xuandun"</span><span>;  </span></span></li>
<li class=""><span>echosql_regcase($name);  </span></li>
</ol>
Copy after login

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/446518.htmlTechArticleWhen learning PHP, you may encounter the problem of judging PHP regular expressions. Here we will introduce the problem of PHP regular expressions The solution is here to share with you. In some query websites...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template