function.ereg.html
来自「php的帮助文档,涉及到PHP的案例和基本语法,以及实际应用内容」· HTML 代码 · 共 185 行
HTML
185 行
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html> <head> <title>Regular expression match</title> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> </head> <body><div style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="function.ereg-replace.html">ereg_replace</a></div> <div class="next" style="text-align: right; float: right;"><a href="function.eregi-replace.html">eregi_replace</a></div> <div class="up"><a href="ref.regex.html">POSIX Regex Functions</a></div> <div class="home"><a href="index.html">PHP Manual</a></div></div><hr /><div id="function.ereg" class="refentry"> <div class="refnamediv"> <h1 class="refname">ereg</h1> <p class="verinfo">(PHP 4, PHP 5)</p><p class="refpurpose"><span class="refname">ereg</span> — <span class="dc-title">Regular expression match</span></p> </div> <div class="refsect1 description"> <h3 class="title">Description</h3> <div class="methodsynopsis dc-description"> <span class="type">int</span> <span class="methodname"><b><b>ereg</b></b></span> ( <span class="methodparam"><span class="type">string</span> <tt class="parameter">$pattern</tt></span> , <span class="methodparam"><span class="type">string</span> <tt class="parameter">$string</tt></span> [, <span class="methodparam"><span class="type">array</span> <tt class="parameter reference">&$regs</tt></span> ] )</div> <p class="simpara"> Searches a <i><tt class="parameter">string</tt></i> for matches to the regular expression given in <i><tt class="parameter">pattern</tt></i> in a case-sensitive way. </p> </div> <div class="refsect1 parameters"> <h3 class="title">Parameters</h3> <p class="para"> <dl> <dt> <span class="term"><i><tt class="parameter">pattern</tt></i></span> <dd> <p class="para"> Case sensitive regular expression. </p> </dd> </dt> <dt> <span class="term"><i><tt class="parameter">string</tt></i></span> <dd> <p class="para"> The input string. </p> </dd> </dt> <dt> <span class="term"><i><tt class="parameter">regs</tt></i></span> <dd> <p class="para"> If matches are found for parenthesized substrings of <i><tt class="parameter">pattern</tt></i> and the function is called with the third argument <i><tt class="parameter">regs</tt></i>, the matches will be stored in the elements of the array <i><tt class="parameter">regs</tt></i>. </p> <p class="para"> $regs[1] will contain the substring which starts at the first left parenthesis; $regs[2] will contain the substring starting at the second, and so on. $regs[0] will contain a copy of the complete string matched. </p> </dd> </dt> </dl> </p> </div> <div class="refsect1 returnvalues"> <h3 class="title">Return Values</h3> <p class="para"> Returns the length of the matched string if a match for <i><tt class="parameter">pattern</tt></i> was found in <i><tt class="parameter">string</tt></i>, or <b><tt>FALSE</tt></b> if no matches were found or an error occurred. </p> <p class="para"> If the optional parameter <i><tt class="parameter">regs</tt></i> was not passed or the length of the matched string is 0, this function returns 1. </p> </div> <div class="refsect1 examples"> <h3 class="title">Examples</h3> <p class="para"> <div class="example"> <p><b>Example #1 <b>ereg()</b> example</b></p> <div class="example-contents"><p> The following code snippet takes a date in ISO format (YYYY-MM-DD) and prints it in DD.MM.YYYY format: </p></div> <div class="example-contents"><div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB"><?php<br /></span><span style="color: #007700">if (</span><span style="color: #0000BB">ereg </span><span style="color: #007700">(</span><span style="color: #DD0000">"([0-9]{4})-([0-9]{1,2})-([0-9]{1,2})"</span><span style="color: #007700">, </span><span style="color: #0000BB">$date</span><span style="color: #007700">, </span><span style="color: #0000BB">$regs</span><span style="color: #007700">)) {<br /> echo </span><span style="color: #DD0000">"$regs[3].$regs[2].$regs[1]"</span><span style="color: #007700">;<br />} else {<br /> echo </span><span style="color: #DD0000">"Invalid date format: $date"</span><span style="color: #007700">;<br />}<br /></span><span style="color: #0000BB">?></span></span></code></div> </div> </div> </p> </div> <div class="refsect1 notes"> <h3 class="title">Notes</h3> <blockquote><p><b class="note">Note</b>: <a href="function.preg-match.html" class="function">preg_match()</a>, which uses a Perl-compatible regular expression syntax, is often a faster alternative to <b>ereg()</b>. <br /> </p></blockquote> <blockquote><p><b class="note">Note</b>: <span class="simpara"> Up to (and including) PHP 4.1.0 <var class="varname">$regs</var> will be filled with exactly ten elements, even though more or fewer than ten parenthesized substrings may actually have matched. This has no effect on <b>ereg()</b>'s ability to match more substrings. If no matches are found, <i>$regs</i> will not be altered by <b>ereg()</b>. </span> </p></blockquote> </div> <div class="refsect1 seealso"> <h3 class="title">See Also</h3> <p class="para"> <ul class="simplelist"> <li class="member"><a href="function.eregi.html" class="function" rel="rdfs-seeAlso">eregi()</a></li> <li class="member"><a href="function.ereg-replace.html" class="function" rel="rdfs-seeAlso">ereg_replace()</a></li> <li class="member"><a href="function.eregi-replace.html" class="function" rel="rdfs-seeAlso">eregi_replace()</a></li> <li class="member"><a href="function.preg-match.html" class="function" rel="rdfs-seeAlso">preg_match()</a></li> <li class="member"><a href="function.strpos.html" class="function" rel="rdfs-seeAlso">strpos()</a></li> <li class="member"><a href="function.strstr.html" class="function" rel="rdfs-seeAlso">strstr()</a></li> <li class="member"><a href="function.quotemeta.html" class="function" rel="rdfs-seeAlso">quotemeta()</a></li> </ul> </p> </div></div><hr /><div style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="function.ereg-replace.html">ereg_replace</a></div> <div class="next" style="text-align: right; float: right;"><a href="function.eregi-replace.html">eregi_replace</a></div> <div class="up"><a href="ref.regex.html">POSIX Regex Functions</a></div> <div class="home"><a href="index.html">PHP Manual</a></div></div></body></html>
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?