Perl 호환 정규 표현식
1. 함수:
preg_match(): 패턴이 문자열과 일치하는지 여부를 나타냅니다. 1회 반환 일치하는 항목이 있으면 0을 반환합니다.
preg_match_all(): 모든 일치 항목 찾기
preg_replace(): 검색 및 바꾸기 기능 구현
2. 단순 모드 :
1. 리터럴: 해석된 대로 정확하게 작성된 값입니다. 예를 들어, 패턴 a는 문자 a와 일치하고, ab는 ab와 일치합니다.
구분 기호: 영숫자 또는 백슬래시를 제외한 모든 문자가 가능하며 패턴의 시작과 끝을 표시하는 데 동일한 문자를 사용해야 합니다. 일반적으로 슬래시(/)를 사용합니다.
메타 문자:
정량자:
공통 문자 카테고리:
3. 연습:
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. 모든 일치 - match.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">'
'
3. 일치 및 대체 - replacement.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 학습 및 실습(16)을 소개합니다. PHP 튜토리얼에 관심이 있는 친구들에게 도움이 되기를 바랍니다.