Perl兼容的正则表达式
一、函数:
preg_match():指示模式是否与字符串匹配,一旦找到一个匹配返回1,否则返回0
preg_match_all():找到所有的匹配
preg_replace():实现查找和替换作用
二、简单的模式:
1. 字面量:是一个值,其书写方式与所解释的完全一样。例如,模式a将匹配字母a,ab将匹配ab等。
定界符:可以是除字母数字或反斜杠外的任何字符,必须使用相同的字符来标记模式的开始和结束。通常使用正斜杠(/)
元字符 :
量词:
常见的字符类别:
三、实践:
1.单一匹配——pcre.php
<code><span><span>html</span><span>lang</span>=<span>"en"</span>></span><span>head</span>><span>meta</span><span>charset</span>=<span>"UTF-8"</span>><span>title</span>>Testing PCRE<span><span>title</span>></span><span><span>head</span>></span><span>body</span>><span><span><?php </span><span># Script 14.1- pcre.php</span><span>if</span>(<span>$_SERVER</span>[<span>'REQUEST_METHOD'</span>] == <span>'POST'</span>) { <span>$pattern</span> = trim(<span>$_POST</span>[<span>'pattern'</span>]); <span>$subject</span> = trim(<span>$_POST</span>[<span>'subject'</span>]); <span>echo</span><span>"<p>The result of checking<br><b>$pattern</b><br>aganist<br>$subject<br>is</p>"</span>; <span>if</span>(preg_match(<span>$pattern</span>, <span>$subject</span>)) { <span>echo</span><span>'TRUE!'</span>; }<span>else</span>{ <span>echo</span><span>'FALSE!'</span>; } } <span>?></span></span><span>form</span><span>action</span>=<span>"pcre.php"</span><span>method</span>=<span>"post"</span>></span><span>p</span>>Regular Expression Pattern: <span>input</span><span>type</span>=<span>"text"</span><span>name</span>=<span>"pattern"</span><span>value</span>=<span>"<?php if(isset($pattern)) echo htmlentities($pattern); ?>"</span><span>size</span>=<span>"40"</span>>(include the delimiters)<span><span>p</span>></span><span>p</span>>Test Subject: <span>input</span><span>type</span>=<span>"text"</span><span>name</span>=<span>"subject"</span><span>value</span>=<span>"<?php if(isset($subject)) echo htmlentities($subject); ?>"</span><span>size</span>=<span>"40"</span> /><span><span>p</span>></span><span>input</span><span>type</span>=<span>"submit"</span><span>name</span>=<span>"submit"</span><span>value</span>=<span>"Test!"</span>><span><span>form</span>></span><span><span>body</span>></span><span><span>html</span>></span></code>
——实现电子邮件验证:电子邮件开始于字母、数字或下划线,以及句点(.)或短划线。
2.所有匹配——matches.php
<code><span><span>html</span><span>lang</span>=<span>"en"</span>></span><span>head</span>><span>meta</span><span>charset</span>=<span>"UTF-8"</span>><span>title</span>>Testing PCRE<span><span>title</span>></span><span><span>head</span>></span><span>body</span>><span><span><?php </span><span># Script 14.1- pcre.php</span><span>if</span>(<span>$_SERVER</span>[<span>'REQUEST_METHOD'</span>] == <span>'POST'</span>) { <span>$pattern</span> = trim(<span>$_POST</span>[<span>'pattern'</span>]); <span>$subject</span> = trim(<span>$_POST</span>[<span>'subject'</span>]); <span>echo</span><span>"<p>The result of checking<br><b>$pattern</b><br>aganist<br>$subject<br>is</p>"</span>; <span>if</span>(preg_match_all(<span>$pattern</span>, <span>$subject</span>, <span>$matches</span>)) { <span>echo</span><span>'TRUE!'</span>; <span>echo</span><span>'<pre class="brush:php;toolbar:false">'.print_r($matches, 1).'
'; }else{ echo'FALSE!'; } } ?>formaction="matches.php"method="post">p>Regular Expression Pattern: inputtype="text"name="pattern"value=""size="40">(include the delimiters)p>p>Test Subject: textareaname="subject"rows="5"cols="40">if(isset($subject)) echo htmlentities($subject); ?>textarea>p>inputtype="submit"name="submit"value="Test!">form>body>html>
3.匹配和替换——replace.php
<code><span><span>html</span><span>lang</span>=<span>"en"</span>></span><span>head</span>><span>meta</span><span>charset</span>=<span>"UTF-8"</span>><span>title</span>>Testing PCRE<span><span>title</span>></span><span><span>head</span>></span><span>body</span>><span><span><?php </span><span># Script 14.1- pcre.php</span><span>if</span>(<span>$_SERVER</span>[<span>'REQUEST_METHOD'</span>] == <span>'POST'</span>) { <span>$pattern</span> = trim(<span>$_POST</span>[<span>'pattern'</span>]); <span>$subject</span> = trim(<span>$_POST</span>[<span>'subject'</span>]); <span>$replace</span> = trim(<span>$_POST</span>[<span>'replace'</span>]); <span>echo</span><span>$subject</span>; <span>echo</span><span>"<p>The result of replacing<br><b>$pattern</b><br>with<br>$replace<br>in<br>$subject<br><br>"</p></span>; <span>if</span>(preg_match(<span>$pattern</span>, <span>$subject</span>)) { <span>echo</span> preg_replace(<span>$pattern</span>, <span>$replace</span>, <span>$subject</span>).<span>''</span>; }<span>else</span>{ <span>echo</span><span>'The pattern was not found!'</span>; } } <span>?></span></span><span>form</span><span>action</span>=<span>"replace.php"</span><span>method</span>=<span>"post"</span>></span><span>p</span>>Regular Expression Pattern: <span>input</span><span>type</span>=<span>"text"</span><span>name</span>=<span>"pattern"</span><span>value</span>=<span>"<?php if(isset($pattern)) echo htmlentities($pattern); ?>"</span><span>size</span>=<span>"40"</span>>(include the delimiters)<span><span>p</span>></span><span>p</span>>Replacement: <span>input</span><span>type</span>=<span>"text"</span><span>name</span>=<span>"replace"</span><span>value</span>=<span>"<?php if(isset($replace)) echo htmlentities($replace); ?>"</span><span>size</span>=<span>"40"</span>><span><span>p</span>></span><span>p</span>>Test Subject: <span>textarea</span><span>name</span>=<span>"subject"</span><span>rows</span>=<span>"5"</span><span>cols</span>=<span>"40"</span>><span><span><?php </span><span>if</span>(<span>isset</span>(<span>$subject</span>)) <span>echo</span> htmlentities(<span>$subject</span>); <span>?></span></span><span><span>textarea</span>></span><span><span>p</span>></span><span>input</span><span>type</span>=<span>"submit"</span><span>name</span>=<span>"submit"</span><span>value</span>=<span>"Test!"</span>></span><span><span>form</span>></span><span><span>body</span>></span><span><span>html</span>></span></code>').addClass('pre-numbering').hide(); $(this).addClass('has-numbering').parent().append($numbering); for (i = 1; i ').text(i)); }; $numbering.fadeIn(1700); }); });
以上就介绍了PHP学习练手(十六),包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。