Home  >  Article  >  Backend Development  >  Learn and practice PHP (16)

Learn and practice PHP (16)

WBOY
WBOYOriginal
2016-07-29 09:06:10874browse


Perl compatible regular expressions


1. Function:

preg_match(): Indicates whether the pattern matches the string. Once a match is found, it returns 1, otherwise it returns 0

preg_match_all(): Find all Match

preg_replace(): implement search and replacement

2. Simple mode:
1. Literal: is a value that is written exactly as it is interpreted. For example, the pattern a will match the letter a, ab will match ab, etc.

  1. Delimiter: Can be any character except alphanumeric or backslash, the same character must be used to mark the beginning and end of the pattern. Usually forward slash (/)

  2. Metacharacter:
    Learn and practice PHP (16)

  3. Quantifier:
    Learn and practice PHP (16)

  4. Common character categories:
    Learn and practice PHP (16)


    3. Practice:

    1. Single match - 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>

- to implement email verification: emails start with letters, numbers or underscores, and periods (.) or dashes.
Learn and practice PHP (16)

2. All matches - 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>

Learn and practice PHP (16)

3. Matches and replacements - 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>

Learn and practice PHP (16)

').addClass('pre-numbering').hide() ; $(this).addClass('has-numbering').parent().append($numbering); for (i = 1; i ').text(i)); }; $numbering.fadeIn(1700); }); });

The above introduces PHP learning and practicing (16), including the content. I hope it will be helpful to friends who are interested in PHP tutorials.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn