Perl相容的正規表示式
一、函數:
preg_match():指示模式是否與字串匹配,一旦找到一個匹配返回1,否則返回0
(preg_match_all(preg匹配
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>
<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>'<div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false">'
'
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>
'). ; $(this).addClass('has-numbering').parent().append($numbering); for (i = 1; i
').text(i)); }; $numbering.fadeIn(1700); }); });以上就介紹了PHP學習練手(十六),包括了方面的內容,希望對PHP教程有興趣的朋友有所幫助。