Home  >  Article  >  Backend Development  >  A brief analysis of PHP regular expression function library_PHP tutorial

A brief analysis of PHP regular expression function library_PHP tutorial

WBOY
WBOYOriginal
2016-07-15 13:26:33988browse

A brief analysis of what is the PHP regular expression function library? Here you are introduced to PHP regular expression function library (POSIX extension) greedy matching. We should pay attention to the features that are not supported in conditional subpatterns and many other POSIX extended regular expression syntax.

Warning when learning PHP regular expression function library:

This type of regular expression cannot be safely used in binary mode. The PCRE library does. Regular expressions are used to perform complex string operations in PHP. Functions that support regular expressions are:

◆ereg()

◆ereg_replace()

◆eregi()

◆eregi_replace()

◆split()

◆spliti()

These functions all accept a regular expression string as their first argument. PHP uses POSIX extended regular expressions defined by POSIX 1003.3. A complete description of POSIX regular expressions can be found in the man page included in the regex directory of the PHP distribution. It can be read with a command like man /usr/local/src/regex/regex.7.

Requirement: No external library files are needed to add support for this extension module.

Installation warning: Do not change TYPE unless you know what you are doing.

To activate regexp support, add --with-regex[=TYPE] when configuring PHP. TYPE can be one of system, apache or php. By default, php is used.

The Windows version of PHP already has built-in support for this extension module. You do not need to load any additional extension libraries to use these functions. Let’s take a look at the specific related operations:

◆Runtime configuration: This extension module does not define any setting instructions.

◆Resource type: This extension module does not define any resource type.

◆Predefined constants: This extension module does not define any constants.

Example of PHP regular expression function:

<ol class="dp-c"><li class="alt"><span><span><?php  </SPAN></SPAN><LI><SPAN class=comment>// 如果在 $string 中任何地方找到 "abc" 则返回 &true; </SPAN><SPAN> </SPAN><LI class=alt><SPAN class=func>ereg</SPAN><SPAN> (</SPAN><SPAN class=string>"abc"</SPAN><SPAN>, </SPAN><SPAN class=vars>$string</SPAN><SPAN>);  </SPAN><LI><SPAN> </SPAN><LI class=alt><SPAN class=comment>// 如果 $string 以 "abc" 开头则返回 &true; </SPAN><SPAN> </SPAN><LI><SPAN class=func>ereg</SPAN><SPAN> (</SPAN><SPAN class=string>"^abc"</SPAN><SPAN>, </SPAN><SPAN class=vars>$string</SPAN><SPAN>);  </SPAN><LI class=alt><SPAN> </SPAN><LI><SPAN class=comment>// 如果 $string 以 "abc" 结尾则返回 &true; </SPAN><SPAN> </SPAN><LI class=alt><SPAN class=func>ereg</SPAN><SPAN> (</SPAN><SPAN class=string>"abc$"</SPAN><SPAN>, </SPAN><SPAN class=vars>$string</SPAN><SPAN>);  </SPAN><LI><SPAN> </SPAN><LI class=alt><SPAN class=comment>// 如果用户浏览器是 Netscape 2,3 或 MSIE 3 则返回 &true; </SPAN><SPAN> </SPAN><LI><SPAN class=func>eregi</SPAN><SPAN> (</SPAN><SPAN class=string>"(ozilla.[23]|MSIE.3)"</SPAN><SPAN>, </SPAN><SPAN class=vars>$HTTP_USER_AGENT</SPAN><SPAN>);  </SPAN><LI class=alt><SPAN> </SPAN><LI><SPAN class=comment>// 将三个空格分隔的单词放入 $regs[1],$regs[2] 和 $regs[3] 中 </SPAN><SPAN> </SPAN><LI class=alt><SPAN class=func>ereg</SPAN><SPAN> ("([[:alnum:]]+) ([[:alnum:]]+)  </SPAN><LI><SPAN> ([[:alnum:]]+)", </SPAN><SPAN class=vars>$string</SPAN><SPAN>,</SPAN><SPAN class=vars>$regs</SPAN><SPAN>);  </SPAN><LI class=alt><SPAN> </SPAN><LI><SPAN class=comment>// 将 <br /> 标记放到 $string 开头 </span><span> </span><li class="alt">
<span class="vars">$string</span><span> = </span><span class="func">ereg_replace</span><span> (</span><span class="string">"^"</span><span>, </span><span class="string">"<br />"</span><span>, </span><span class="vars">$string</span><span>);  </span>
</li>
<li><span> </span></li>
<li class="alt">
<span class="comment">// 将 <br /> 标记放到 $string 结尾 </span><span> </span>
</li>
<li>
<span class="vars">$string</span><span> = </span><span class="func">ereg_replace</span><span> (</span><span class="string">"$"</span><span>, </span><span class="string">"<br />"</span><span>, </span><span class="vars">$string</span><span>);  </span>
</li>
<li class="alt"><span> </span></li>
<li>
<span class="comment">// 删除 $string 中的所有换行符 </span><span> </span>
</li>
<li class="alt">
<span class="vars">$string</span><span> = </span><span class="func">ereg_replace</span><span> (</span><span class="string">"n"</span><span>, </span><span class="string">""</span><span>, </span><span class="vars">$string</span><span>);  </span>
</li>
<li><span>?>   </span></li></span></li></ol>

This is the introduction to PHP regular expression function. I hope you can understand and learn from it. PHP regular expression functions help.


www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/446591.htmlTechArticleA brief analysis of what is the PHP regular expression function library? Here you are introduced to PHP regular expression function library (POSIX extension) greedy matching. We should pay attention to the conditional sub-pattern and many other...
Statement:
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