 |
|
|  |
PCRE Á¤±ÔÇ¥Çö½Ä ¿¹Á¦·Î °³³äÀâ±â. |
|
|
 |
11³â Àü |
PCRE Á¤±ÔÇ¥Çö½Ä ¿¹Á¦·Î °³³äÀâ±â.
http://kr2.php.net/manual/en/ref.pcre.php
http://kr2.php.net/manual/en/reference.pcre.pattern.syntax.php
http://kr2.php.net/manual/en/reference.pcre.pattern.modifiers.php
À§ ¸µÅ©°¡ ÀÌÇØ°¡ ¾ÈµÇ¾îµµ ½ºÅ©·ÑÀ» õõÈ÷ ³»·Á°¡¸ç Çѹø ÀоîÁÖ°í º»¹®À» ÀоîÁÖ¼¼¿ä.
º»¹®¿¡ ¾ø´Â°Ç ¹¯Áö ¸¶¼¼¿ä. ¸ð¸¨´Ï´Ù.
pattern syntax
1. [0-9] ´Â \d ·Î Ç¥ÇöÇϴ°ÍÀÌ Æí¸®ÇÏ´Ù.
±Û ¼öÁ¤½Ã¿¡ ±Û¹øÈ£¸¦ üũÇØ º¸ÀÚ.
<?php
if(!isset($_POST['seq']) || !preg_match('/^[1-9]\d*$/', $_POST['seq'])) { // ½ÃÄö½º´Â 1ºÎÅÍ ½ÃÀÛÇϹǷΠóÀ½Àº 1-9 ÀÌ´Ù.
header('HTTP/1.1 400');
echo str_repeat('ÇØÅ·Àº »çÀý<br />', 200);
exit;
}
?>
2. ' '(°ø¹é)Àº ¾Ë¾Æº¸±â ½±µµ·Ï \s ·Î ½áÁÖ¸é ÁÁ´Ù.
3. \(¹é½½·¡½Ã) ¸¦ Ç¥ÇöÇÒ¶§ ¹ö±×ÀÎÁö \\ ·Î ÇÏ¸é ¾ÈµÈ´Ù.
\\\ \\\\\ ÀÌ·±½ÄÀ¸·Î Ȧ¼ö°¡ µÇ´Âµ¥ ±×³É \x5c ·Î hex Ç¥ÇöÇØÁÖ¸é ÆíÇÏ´Ù.
4. (?) ·Î ¸ÅĪ°á°ú¿¡ Ç¥ÇöµÇÁö ¾Ê°í Á¶°Ç¸¸ Ç¥ÇöÇÒ ¼ö ÀÖ´Ù.
¹®ÀÚ´Â *³ª + µîÀÇ °¡º¯±æÀ̸¦ Áö¿øÇÏÁö ¾Ê´Â´Ù.
(?=¹®ÀÚ) (?!¹®ÀÚ) : ¹®ÀÚ¿ ±æÀÌ °è»ê¿¡ ¹Ý¿µµÇÁö ¾Ê°í Á¶°Ç¸¸ °Ë»çÇÑ´Ù.
<?php
$text = 'abcd';
echo preg_replace('/(?=b)./', 'e', $text); // Á¶°Ç¿¡ ¸Â´Â°÷¿¡¼ ÇѱÛÀÚ °¡Á®¿À±â.
?>
aecd
(?<=¹®ÀÚ) (?<!¹®ÀÚ) : ¹®ÀÚ¿ ±æÀÌ °è»ê¿¡ ¹Ý¿µµÈ´Ù.
<?php
$text = 'abcd';
echo preg_replace('/(?<=b)./', 'e', $text); // Á¶°ÇÀÌ ¸Â´Â°÷ ´ÙÀ½ ÇѱÛÀÚ °¡Á®¿À±â.
?>
abed
µû¿ÈÇ¥·Î °¨½Ñ ¹®ÀÚ¿¿¡ \ °¡ À־ Á¤È®ÇÏ°Ô »Ì¾Æ³» º¸ÀÚ.
<?php
$text = <<<TEXT
<script type="text/javascript">
document.body.innerHTML += "<div style="font-size:12px; color:white; background-color:black;\">";
document.body.innerHTML += '\'¿¹Á¦ ¸¸µé±â ±ÍÂú³×...\'';
document.body.innerHTML += "</div>";
TEXT;
$cnt = preg_match_all('@(?<!\x5c)([\'"])(.*?)(?<!\x5c)\1@', $text, $matches);
print_r($matches[2]);
?>
Array
(
[0] => text/javascript
[1] => <div style="font-size:12px; color:white; background-color:black;\">
[2] => \'¿¹Á¦ ¸¸µé±â ±ÍÂú³×...\'
[3] => </div>
)
5. ÁٹٲÞÀÌ \r\n ÀÎÁö \n ÀÎÁö ºñ±³Çϱ⠱ÍÂúÀ¸¹Ç·Î [\r\n]+ ȤÀº(\r?\n) À¸·Î Ç¥ÇöÇÏÀÚ.
6. UTF-8 À» »ç¿ëÇÑ´Ù¸é /u modifier ¿¡¼ \x{ac00} °°Àº Ç¥ÇöÀÌ °¡´ÉÇØÁø´Ù.
[\x{ac00}-\x{d7af}] ÀÌ·¸°Ô ¾²¸é [°¡-¤¾¤Ó¤¾] ÀÌ´Ù.
°¡-¤¾¤Ó¤¾ ¸¸À̶ó¸é ±×³É UTF-8 ·Î ½áµµ µ¿ÀÛÇϴµ¥,
Ÿ ¾ð¾î³ª Ư¼ö¹®ÀÚ °°Àº °æ¿ì¿¡ Àß ¾ÈµÉ¼ö°¡ ÀÖ´Ù.
±×·²¶§¸¦ À§ÇØ ±â¾ïÇØ µÎÀÚ. (ÄÚµå´Â UCS-2)
<?php
$text = 'abcd°¡³ª´Ù¶ó2345';
$cnt = preg_match_all('/./u', $text, $matches); // ±ÛÀÚ ´ÜÀ§·Î Àß¶ó °¡Á®¿Â´Ù.
print_r($matches);
echo preg_replace('/[^\x{ac00}-\x{d7af}]+/u', '', $text); // Çѱ¹¾î¸¸ ³²±ä´Ù.
?>
Array
(
[0] => Array
(
[0] => a
[1] => b
[2] => c
[3] => d
[4] => °¡
[5] => ³ª
[6] => ´Ù
[7] => ¶ó
[8] => 2
[9] => 3
[10] => 4
[11] => 5
)
)
°¡³ª´Ù¶ó
7. UTF-8 ¿¡¼ ÇÑ°¡Áö ´õ ÀÌÁ¡ÀÌ ÀÖ´Ù. \p °¡ ±×°ÍÀÌ´Ù.
<?php
$text = '°¡ ³ª¡¡´Ù'; // ³ª¿Í ´Ù »çÀÌ´Â ¹À½ºóÄÀÌ´Ù.
echo preg_replace('/\p{Zs}/u', '', $text);
?>
°¡³ª´Ù
ÀÌ·¸µí ¹®ÀÚµéÀ» ¼Ó¼º¿¡ µû¶ó ±×·ìÀ¸·Î ³ª´©¾î Ç¥±âÇÒ ¼ö ÀÖ´Ù.
À§ÀÇ ¿¹Á¦´Â Space separator ÀÌ°í, /u ¼±¾ð¿¡ ÀÇÇØ UTF-8 ·Î Á¤ÀÇµÇ¾î ¹À½ºóĵµ ÇÔ²² 󸮵Ǿú´Ù.
°ø¹éÀÌ ¾Æ´Ñ°ÍÀ» ¸ÅĪÇÏ·Á¸é [^\p{Zs}] ·Î Çصµ µÇ°ÚÁö¸¸ \P ¶ó´Â Ç¥ÇöÀ¸·Î ½±°Ô Ç¥ÇöÇÒ ¼öµµ ÀÖ´Ù.
\pZ À¸·Î ÇÑ´Ù¸é ¿£ÅÍ, ÅÇ µîµµ ¸ðÁ¶¸® ¸ÅĪµÉ°ÍÀÌ´Ù.
¸Þ´º¾ó¿¡ »ç¿ëµÈ ¸íĪÀº http://unicode.org ¿¡¼ È®ÀÎÇÒ ¼ö ÀÖ´Ù.
pattern modifier
1. /m
¿©·¯ ÁÙÀÌ ÀÖ°í ^ ³ª $ ¸¦ ¸Å ÁÙÀÇ ½ÃÀÛ°ú ³¡À¸·Î ÇÏ°íÀÚ ÇÑ´Ù¸é /m À» »ç¿ëÇÑ´Ù.
<?php
$text = <<<TEXT
1. °¡
2. ³ª
3. ´Ù
TEXT;
preg_match_all('/(^(\d+)|(.)$)/mu', $text, $matches); // ¾ÕÀÇ ¼ö¿Í µÚÀÇ ÇѱÛÀÚ¸¦ °¡Á®¿Â´Ù.
print_r($matches[2]);
print_r($matches[3]);
?>
Array
(
[0] => 1
[1] =>
[2] => 2
[3] =>
[4] => 3
[5] =>
)
Array
(
[0] =>
[1] => °¡
[2] =>
[3] => ³ª
[4] =>
[5] => ´Ù
)
2. .* ·Î ¿©·¯ÁÙÀ» ´Ù ¸ÅĪÇÒ ¶§´Â /s ¸¦ »ç¿ëÇÑ´Ù.
3. ű׸¦ Ç¥ÇöÇÒ ¶§ ó·³ / °¡ ¸¹Àº °÷¿¡¼´Â @ ó·³ Àß ¾È³ª¿À´Â°ÍÀ¸·Î ÆÐÅϱ¸ºÐÀÚ¸¦ ¹Ù²Û´Ù.
<?php
$text = <<<TEXT
<a href="http://xenosi.de/" target="_blank" title="¼ÛÈ¿ÁøÀÇ È¨ÆäÀÌÁö">
¾Æ¹«°Íµµ
¾ø¾î¿ä.
</a>
TEXT;
preg_match('@<a\s.*?>(.*?)</a>@su', $text, $matches);
print_r($matches[1]);
?>
¾Æ¹«°Íµµ
¾ø¾î¿ä.
4. ±âº»ÀûÀ¸·Î Á¤±Ô½ÄÀº ¸ÅÄ¡µÇ´Â Ç¥ÇöÁß °¡Àå Å« °ªÀ» °¡Á®¿Â´Ù.
°¡Àå ÀÛÀº °ªÀ» °¡Á®¿À´Â ¹æ½ÄÀº 3°¡Áö°¡ ÀÖ´Ù.
<?php
$text = <<<TEXT
<a href="http://xenosi.de/" target="_blank" title="¼ÛÈ¿ÁøÀÇ È¨ÆäÀÌÁö">
¾Æ¹«°Íµµ
¾ø¾î¿ä.
</a>
<a href="http://xenosi.de/" target="_blank" title="¼ÛÈ¿ÁøÀÇ È¨ÆäÀÌÁö">
¾Æ¹«°Íµµ
¾ø¾î¿ä.
</a>
TEXT;
echo "--1--\n";
preg_match('@<a.*?>(.*)</a>@su', $text, $matches); // ¸ðµÎ ´Ù °¡Á®¿Ã °ÍÀÌ´Ù.
print_r($matches[1]);
echo "--2--\n";
preg_match('@<a.*?>(.*?)</a>@su', $text, $matches); // °¡Àå °£ÆíÇÏ°í ¾Ë¾Æº¸±â ÁÁ´Ù. Ãßõ.
print_r($matches[1]);
echo "--3--\n";
preg_match('@<a(?U).*>(?U)(.*)</a>@su', $text, $matches); // pattern modifier ½ÄÀ» ºÎºÐ¸¸ Àû¿ëÇϴµ¥ ¹üÀ§¸¦ ¾Ë¾Æº¸±â°¡ ÁÁÁö ¾Ê´Ù.
print_r($matches[1]);
echo "--4--\n";
preg_match('@<a.*>(.*)</a>@Usu', $text, $matches); // ½Ä Àüü¿¡ Àû¿ëµÈ´Ù.
print_r($matches[1]);
?>
--1--
¾Æ¹«°Íµµ
¾ø¾î¿ä.
</a>
<a href="http://xenosi.de/" target="_blank" title="¼ÛÈ¿ÁøÀÇ È¨ÆäÀÌÁö">
¾Æ¹«°Íµµ
¾ø¾î¿ä.
--2--
¾Æ¹«°Íµµ
¾ø¾î¿ä.
--3--
¾Æ¹«°Íµµ
¾ø¾î¿ä.
--4--
¾Æ¹«°Íµµ
¾ø¾î¿ä.
5. ´ë¼Ò¹®ÀÚ ±¸º° ¾ø¾Ö´Â /i ´Â ¼³¸íÇÏ¸é ¼Õ°¡¶ô ¾ÆÇÁ´Ù.
ÀÀ¿ë ¿¹Á¦.
<?php
$text = <<<TEXT
<a href="http://xenosi.de/" target="_blank" title ="¼ÛÈ¿ÁøÀÇ È¨ÆäÀÌÁö">
¾Æ¹«°Íµµ
¾ø¾î¿ä.
</a>
<a href="http://xenosi.de/?a=°¡&b=³ª&c=´Ù&d&e" target="_blank" title = "¼ÛÈ¿ÁøÀÇ È¨ÆäÀÌÁö">
¾Æ¹«°Íµµ
¾ø¾î¿ä.
</a>
TEXT;
if(preg_match('@<a\s(.*?)>@s', $text, $matches)) {
$cnt = preg_match_all('@([^\pZ=]+?)\pZ*=\pZ*([\'"])(.*?)\2@us', $matches[1], $matches2);
print_r($matches2[1]);
print_r($matches2[3]);
}
echo preg_replace_callback('@href=([\'"])((https?://[^\?]+\??)?(.*?))\1@', 'urlencode_callback', $text);
function urlencode_callback($matches)
{
return 'href='.$matches[1].$matches[3].preg_replace_callback('/(.*?)=([^&]+)/', 'urlencode_value_callback', $matches[4]).$matches[1];
}
function urlencode_value_callback($matches)
{
return $matches[1].'='.urlencode($matches[2]);
}
?>
Array
(
[0] => href
[1] => target
[2] => title
)
Array
(
[0] => http://xenosi.de/
[1] => _blank
[2] => ¼ÛÈ¿ÁøÀÇ È¨ÆäÀÌÁö
)
<a href="http://xenosi.de/" target="_blank" title ="¼ÛÈ¿ÁøÀÇ È¨ÆäÀÌÁö">
¾Æ¹«°Íµµ
¾ø¾î¿ä.
</a>
<a href="http://xenosi.de/?a=%EA%B0%80&b=%EB%82%98&c=%EB%8B%A4&d&e" target="_blank" title = "¼ÛÈ¿ÁøÀÇ È¨ÆäÀÌÁö">
¾Æ¹«°Íµµ
¾ø¾î¿ä.
</a>
À妽ÌÀ» À§ÇÑ ´Ü¾î ÃßÃâ ¿¹Á¦
http://www.phpschool.com/gnuboard4/bbs/board.php?bo_table=tipntech&wr_id=50824&sca=&sfl=wr_name%7C%7Csubject&stx=%BC%DB%C8%BF%C1%F8&sop=and&page=2
³¡À¸·Î ÀÌ ¹®¼¸¦ ÇÏÀ϶óÀÌÆà Çϱâ À§ÇÑ ¼Ò½º¸¦ ºÙÀÌ°í ¸¶Ä£´Ù.
¿øº»±ÛÀº ÷ºÎÆÄÀÏ·Î ¿Ã¸°´Ù.
<?php
$pattern[] = '/&/';
$replacement[] = '&';
$pattern[] = '/</';
$replacement[] = '<';
$pattern[] = '/>/';
$replacement[] = '>';
$pattern[] = '/(\r?\n)/';
$replacement[] = "<br />\n";
$pattern[] = '/\s{4}|\t/';
$replacement[] = ' ';
$pattern[] = '@^\*\*(.*)<br />@m';
$replacement[] = '<h3>\1</h3>';
$pattern[] = '@^\*(.*)<br />@m';
$replacement[] = '<h2>\1</h2>';
$pattern[] = '@(https?://[^\s<"]+)@';
$replacement[] = '<a href="\1" target="_blank">\1</a>';
$pattern[] = '@(<\?php<br />.*?\?><br />)@s';
$replacement[] = '<div style="background-color:black; color:white; padding:10px; font-family:GungSuhChe,MonoSpace;">\1</div>';
$pattern[] = '@<-<br />(.*?)-><br />@s';
$replacement[] = '<div style="background-color:#003300; color:white; padding:10px; font-family:GungSuhChe,MonoSpace;">\1</div>';
$pattern[] = '@<#<br />(.*?)#><br />@s';
$replacement[] = '<div style="background-color:#330000; color:white; padding:10px; font-weight:bold; font-family:GungSuhChe,MonoSpace;">\1</div>';
echo preg_replace($pattern, $replacement, $_POST['code']);
exit;
?>
ÃֽŠphp ´Â ¸Þ¸ð¸® °ü¸®¸¦ À§ÇØ ¾Æ·¡¿Í °°Àº ¼³Á¤ÀÌ Æ÷ÇԵǾú´Ù.
Å« ÅؽºÆ®¿¡ Àû¿ëÇÒ °æ¿ì ¾Æ¹« ¿À·ù¾øÀÌ ¾ÈµÇ¾î¹ö¸®´Â Çö»óÀÌ ÀÖÀ¸¸é,
ÀÌ °ªÀ» ÀǽÉÇÏÀÚ.
pcre.backtrack_limit integer
pcre.recursion_limit integer
|
|
̵̧ : 575 |
̵̧
¸ñ·Ï
|
|
|  |
|