PHP學習練手(十六)

WBOY
發布: 2016-07-29 09:06:10
原創
869 人瀏覽過


Perl相容的正規表示式


一、函數:

preg_match():指示模式是否與字串匹配,一旦找到一個匹配返回1,否則返回0

(preg_match_all(preg匹配

preg_replace():實作尋找與替換作用

二、簡單的模式:
1. 字面量:是一個值,其書寫方式與所解釋的完全一樣。例如,模式a將匹配字母a,ab將匹配ab等。

  1. 定界符:可以是除字母數字或反斜線外的任何字符,必須使用相同的字符來標記模式的開始和結束。通常使用正斜線(/)

  2. 元字元 :
    PHP學習練手(十六)

  3. 量詞:
    PHP學習練手(十六)

  4. 三、實踐:
    PHP學習練手(十六)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>'<div class="code" style="position:relative; padding:0px; margin:0px;"><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>
PHP學習練手(十六)

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>
登入後複製

PHP學習練手(十六)

'). ; $(this).addClass('has-numbering').parent().append($numbering); for (i = 1; i

').text(i)); }; $numbering.fadeIn(1700); }); });

PHP學習練手(十六) 以上就介紹了PHP學習練手(十六),包括了方面的內容,希望對PHP教程有興趣的朋友有所幫助。

相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門推薦
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板