<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> <title>Examples</title> </head> <body><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="regex.constants.html">Predefined Constants</a></div> <div class="next" style="text-align: right; float: right;"><a href="ref.regex.html">POSIX Regex Functions</a></div> <div class="up"><a href="book.regex.html">POSIX Regex</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div><hr /><div id="regex.examples" class="appendix"> <h1>Examples</h1> <p class="para"> <div class="example" id="example-4805"> <p><strong>Example #1 Regular Expression Examples</strong></p> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br /></span><span style="color: #FF8000">// Returns true if "abc" is found anywhere in $string.<br /></span><span style="color: #0000BB">ereg</span><span style="color: #007700">(</span><span style="color: #DD0000">"abc"</span><span style="color: #007700">, </span><span style="color: #0000BB">$string</span><span style="color: #007700">); <br /><br /></span><span style="color: #FF8000">// Returns true if "abc" is found at the beginning of $string.<br /></span><span style="color: #0000BB">ereg</span><span style="color: #007700">(</span><span style="color: #DD0000">"^abc"</span><span style="color: #007700">, </span><span style="color: #0000BB">$string</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">// Returns true if "abc" is found at the end of $string.<br /></span><span style="color: #0000BB">ereg</span><span style="color: #007700">(</span><span style="color: #DD0000">"abc$"</span><span style="color: #007700">, </span><span style="color: #0000BB">$string</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">// Returns true if client browser is Netscape 2, 3 or MSIE 3.<br /></span><span style="color: #0000BB">eregi</span><span style="color: #007700">(</span><span style="color: #DD0000">"(ozilla.[23]|MSIE.3)"</span><span style="color: #007700">, </span><span style="color: #0000BB">$_SERVER</span><span style="color: #007700">[</span><span style="color: #DD0000">"HTTP_USER_AGENT"</span><span style="color: #007700">]);<br /><br /></span><span style="color: #FF8000">// Places three space separated words into $regs[1], $regs[2] and $regs[3].<br /></span><span style="color: #0000BB">ereg</span><span style="color: #007700">(</span><span style="color: #DD0000">"([[:alnum:]]+) ([[:alnum:]]+) ([[:alnum:]]+)"</span><span style="color: #007700">, </span><span style="color: #0000BB">$string</span><span style="color: #007700">, </span><span style="color: #0000BB">$regs</span><span style="color: #007700">); <br /><br /></span><span style="color: #FF8000">// Put a <br /> tag at the beginning of $string.<br /></span><span style="color: #0000BB">$string </span><span style="color: #007700">= </span><span style="color: #0000BB">ereg_replace</span><span style="color: #007700">(</span><span style="color: #DD0000">"^"</span><span style="color: #007700">, </span><span style="color: #DD0000">"<br />"</span><span style="color: #007700">, </span><span style="color: #0000BB">$string</span><span style="color: #007700">); <br /><br /></span><span style="color: #FF8000">// Put a <br /> tag at the end of $string.<br /></span><span style="color: #0000BB">$string </span><span style="color: #007700">= </span><span style="color: #0000BB">ereg_replace</span><span style="color: #007700">(</span><span style="color: #DD0000">"$"</span><span style="color: #007700">, </span><span style="color: #DD0000">"<br />"</span><span style="color: #007700">, </span><span style="color: #0000BB">$string</span><span style="color: #007700">); <br /><br /></span><span style="color: #FF8000">// Get rid of any newline characters in $string.<br /></span><span style="color: #0000BB">$string </span><span style="color: #007700">= </span><span style="color: #0000BB">ereg_replace</span><span style="color: #007700">(</span><span style="color: #DD0000">"\n"</span><span style="color: #007700">, </span><span style="color: #DD0000">""</span><span style="color: #007700">, </span><span style="color: #0000BB">$string</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> </div> </p> </div> <hr /><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="regex.constants.html">Predefined Constants</a></div> <div class="next" style="text-align: right; float: right;"><a href="ref.regex.html">POSIX Regex Functions</a></div> <div class="up"><a href="book.regex.html">POSIX Regex</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div></body></html>