ȸ¿ø°¡ÀԡžÆÀ̵ð/ºñ¹øã±â
ȨÀ¸·Î

PCRE Á¤±ÔÇ¥Çö½Ä ¿¹Á¦·Î °³³äÀâ±â.
10³â Àü
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


ÃßõÃßõ : 513 Ãßõ ¸ñ·Ï
¹øÈ£ Á¦¸ñ
2,885
input ÀÔ·Â ÇÊµå ¾ÕµÚ °ø¹é ½Ç½Ã°£ Á¦°Å
2,884
Placeholder Æ÷Ä¿½º½Ã °¨Ãß±â
2,883
MySQL Áߺ¹µÈ µ¥ÀÌÅ͸¦ »èÁ¦
2,882
MySQL Áߺ¹ µ¥ÀÌÅÍ È®ÀÎ
2,881
sessionStorage.getItem ¿Í sessionStorage.setItem
2,880
Á¦ÀÌÄõ¸® ·£´ýÀ¸·Î ¹è°æ»ö º¯°æ
2,879
preg match¿¡ °üÇÑ Á¤±Ô½Ä
2,878
Stream an audio file with MediaPlayer ¿Àµð¿À ÆÄÀÏ ½ºÆ®¸®¹Ö Çϱâ
2,877
Audio Streaming PHP Code
2,876
PHP $ SERVER ȯ°æ º¯¼ö Á¤¸®
2,875
Vimeo (ºñ¸Þ¿À) API ¸¦ »ç¿ëÇÏ¿© Ç÷¹À̾î ÄÁÆ®·ÑÇϱâ
2,874
iframe »ç¿ë½Ã ÇÏ´Ü¿¡ ¹ß»ýÇÏ´Â °ø¹é Á¦°Å¹æ¹ý
2,873
¾ÆÀÌÇÁ·¹ÀÓ(iframe) Àüüȭ¸é °¡´ÉÇÏ°Ô Çϱâ
2,872
ºÎÆ®½ºÆ®·¦(bootstrapk)¿¡¼­ »ç¿ëÇÏ´Â class¸í Á¤¸®
2,871
ºÎÆ®½ºÆ®·¦ CSS
2,870
Å©·Ò¿¡¼­ ¸¶Áø Á¶Àý
2,869
PHP ÇöÀç ÆäÀÌÁöÀÇ µµ¸ÞÀθíÀ̳ª urlµîÀÇ Á¤º¸ ¾Ë¾Æ¿À±â
2,868
PHP preg match all()
2,867
PHP ·Î À¥ÆäÀÌÁö ±Ü¾î¿À±â ¸ðµç ¹æ¹ý ÃÑÁ¤¸®!
2,866
[PHP] ¿ø°ÝÁö ÆÄÀÏ ÁÖ¼Ò ³ëÃâ ¾ÈÇÏ°í curl·Î ´Ù¿î·Îµå ¹Þ±â
2,865
PHP ÇÔ¼ö Á¤¸®
2,864
¾ÆÀÌÇÁ·¹ÀÓ(iframe) ºñÀ² À¯ÁöÇϸ鼭 Å©±â Á¶ÀýÇÏ´Â ¹æ¹ý
2,863
PHP ¹è¿­¿¡¼­ ¹«ÀÛÀ§·Î Çϳª »Ì¾ÆÁÖ´Â array rand() ÇÔ¼ö
2,862
PHP Á¤±Ô½Ä Á¤¸®
2,861
PHP Á¤±Ô½ÄÀ» È°¿ëÇÑ ÅÂ±× ¹× ƯÁ¤ ¹®ÀÚ¿­ Á¦°Å ¹× ÃßÃâ ¹æ¹ý
2,860
php Å©·Ñ¸µ ¶Ç´Â ÆÄ½Ì ÇÔ¼ö, Á¤±Ô½Ä ¸ðÀ½
2,859
Á¦ÀÌÄõ¸® ±âº» ¸í·É¾î
2,858
À¥ÆäÀÌÁö °¡·Î ¸ðµå¼¼·Î ¸ðµå ÀνÄÇϱâ
2,857
¸ð¹ÙÀÏ À¥ È­¸é °­Á¦ ȸÀü(°¡·Î¸ðµå °íÁ¤)
2,856
[HTML5]¿¡¼­ frameset ´ëü ¹æ¹ý°ú iframe ¼Ó¼º
¸ñ·Ï
¹ÂÁ÷Æ®·ÎÆ® ºÎ»ê±¤¿ª½Ã ºÎ»êÁø±¸ °¡¾ßµ¿ ¤Ó °³ÀÎÁ¤º¸Ãë±Þ¹æħ
Copyright ¨Ï musictrot All rights reserved.