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


preg match
9³â Àü

(PHP 4, PHP 5)

preg_match — Á¤±ÔÇ¥Çö½Ä ¸ÅÄ¡¸¦ ¼öÇà


¼³¸í

int preg_match ( string $pattern , string $subject [, array &$matches [, int $flags [, int $offset ]]] )

pattern ¿¡ ÁÖ¾îÁø Á¤±ÔÇ¥Çö½ÄÀ» subject ¿¡¼­ ã½À´Ï´Ù.


Àμö


pattern  
Ž»öÇÒ ÆÐÅÏ ¹®ÀÚ¿­.
subject  
ÀÔ·Â ¹®ÀÚ¿­.
matches  
matches °¡ ÁÖ¾îÁö¸é, °Ë»ö °á°ú¸¦ ä¿ö³Ö½À´Ï´Ù. $matches[0]´Â Àüü ÆÐÅÏ ÅؽºÆ®°¡ µé¾î°¡°í, $matches[1]ºÎÅÍ °ýÈ£·Î µÑ·¯½ÎÀÎ ¼­ºê ÆÐÅÏÀ» ä¿ö³Ö½À´Ï´Ù.
flags  
flags ´Â ´ÙÀ½°ú °°Àº Ç÷¡±×¸¦ »ç¿ëÇÒ ¼ö ÀÖ½À´Ï´Ù:
PREG_OFFSET_CAPTURE ÀÌ Ç÷¡±×¸¦ ³Ñ±â¸é, ¸ðµç ¸ÅÄ¡¿¡ ´ëÇÑ ¹®ÀÚ¿­ ½ÃÀÛ À§Ä¡¸¦ ÇÔ²² ¹ÝȯÇÕ´Ï´Ù. ¹Ýȯ°ªÀ» 0¿¡ ¸ÅÄ¡ÇÑ ¹®ÀÚ¿­À» °¡Áö°í, 1¿¡ ¹®ÀÚ¿­ ½ÃÀÛ À§Ä¡¸¦ °¡Áö´Â ¹è¿­À» ¿ø¼Ò·Î °®´Â ¹è¿­·Î º¯°æÇÏ´Â Á¡¿¡ ÁÖÀÇÇϽʽÿÀ.  

offset  
ÀϹÝÀûÀ¸·Î, °Ë»öÀº ¸ñÇ¥ ¹®ÀÚ¿­ÀÇ Ã³À½¿¡¼­ ½ÃÀÛÇÕ´Ï´Ù. ¼±ÅÃÀûÀÎ Àμö offset À¸·Î °Ë»öÀ» ½ÃÀÛÇÒ ´Ù¸¥ À§Ä¡¸¦ ÁöÁ¤ÇÒ ¼ö ÀÖ½À´Ï´Ù. (¹ÙÀÌÆ® ´ÜÀ§)


Note: offset À» »ç¿ëÇÏ´Â °ÍÀº preg_match()ÀÇ ÁÖÁ¦ ¹®ÀÚ¿­·Î substr($subject, $offset)À» »ç¿ëÇÏ´Â °Í°ú ´Ù¸¨´Ï´Ù. pattern ¿¡ ^, $, (?<=x) µîÀÇ ´ÜÁ¤À» Æ÷ÇÔÇÒ ¼ö Àֱ⠶§¹®ÀÔ´Ï´Ù. ºñ±³:



<?php
$subject = "abcdef";
$pattern = '/^def/';
preg_match($pattern, $subject, $matches, PREG_OFFSET_CAPTURE, 3);
print_r($matches);
?>  

À§ ¿¹Á¦ÀÇ Ãâ·Â:


Array
(
)


¹Ý¸é ÀÌ ¿¹Á¦´Â


<?php
$subject = "abcdef";
$pattern = '/^def/';
preg_match($pattern, substr($subject,3), $matches, PREG_OFFSET_CAPTURE);
print_r($matches);
?>  

´ÙÀ½ °á°ú°¡ µË´Ï´Ù


Array
(
    [0] => Array
        (
            [0] => def
            [1] => 0
        )

)








¹Ýȯ°ª

preg_match()´Â pattern ÀÌ ¸ÅÄ¡µÈ Ƚ¼ö¸¦ ¹ÝȯÇÕ´Ï´Ù. ÀÌ´Â 0(¸ÅÄ¡ ¾øÀ½)À̳ª 1ÀÔ´Ï´Ù. preg_match()´Â óÀ½ ¸ÅÄ¡ ÈÄ¿¡ °Ë»öÀ» ÁßÁöÇϱ⠶§¹®ÀÔ´Ï´Ù. ´ëÁ¶ÀûÀ¸·Î, preg_match_all()´Â subject ÀÇ ³¡±îÁö °è¼ÓÇؼ­ ½ÇÇàÇÕ´Ï´Ù. ¿¡·¯°¡ ¹ß»ýÇϸé, preg_match()´Â FALSE¸¦ ¹ÝȯÇÕ´Ï´Ù.


º¯°æÁ¡




¹öÀü

¼³¸í


4.3.3 offset Àμö Ãß°¡  
4.3.0 PREG_OFFSET_CAPTURE Ç÷¡±× Ãß°¡  
4.3.0 flags Àμö Ãß°¡  




¿¹Á¦




Example #1 ¹®ÀÚ¿­ "php" ã±â


<?php
// ÆÐÅÏ ±¸ºÐÀÚ µÚÀÇ "i"´Â ´ë¼Ò¹®ÀÚ¸¦ ±¸º°ÇÏÁö ¾Ê°Ô ÇÕ´Ï´Ù.
if (preg_match("/php/i", "PHP is the web scripting language of choice.")) {
    echo "¹ß°ßÇÏ¿´½À´Ï´Ù.";
} else {
    echo "¹ß°ßÇÏÁö ¸øÇß½À´Ï´Ù.";
}
?>  






Example #2 ´Ü¾î "Web" ã±â


<?php
/* ÆÐÅÏ¿¡¼­ \b´Â ´Ü¾î¸¦ Áö½ÃÇÕ´Ï´Ù. ´Ü¾î "web"¸¸ ¸ÅÄ¡ÇÏ°í,
* "webbing"À̳ª "cobweb" µîÀÇ ºÎºÐÀûÀÎ °æ¿ì¿¡´Â ¸ÅÄ¡ÇÏÁö ¾Ê½À´Ï´Ù. */
if (preg_match("/\bweb\b/i", "PHP is the web scripting language of choice.")) {
    echo "¹ß°ßÇÏ¿´½À´Ï´Ù.";
} else {
    echo "¹ß°ßÇÏÁö ¸øÇß½À´Ï´Ù.";
}

if (preg_match("/\bweb\b/i", "PHP is the website scripting language of choice.")) {
    echo "¹ß°ßÇÏ¿´½À´Ï´Ù.";
} else {
    echo "¹ß°ßÇÏÁö ¸øÇß½À´Ï´Ù.";
}
?>  






Example #3 URL¿¡¼­ µµ¸ÞÀÎ À̸§ ¾ò±â


<?php
// URL¿¡¼­ È£½ºÆ® À̸§ ¾ò±â
preg_match('@^(?:http://)?([^/]+)@i',
    "http://www.php.net/index.html", $matches);
$host = $matches[1];

// È£½ºÆ® À̸§¿¡¼­ ¸¶Áö¸· µÎ ¼¼±×¸àÆ® ¾ò±â
preg_match('/[^.]+\.[^.]+$/', $host, $matches);
echo "µµ¸ÞÀÎ À̸§Àº: {$matches[0]}\n";
?>  


À§ ¿¹Á¦ÀÇ Ãâ·Â:


µµ¸ÞÀÎ À̸§Àº: php.net







Example #4 À̸§ ÀÖ´Â ¼­ºêÆÐÅÏ »ç¿ëÇϱâ


<?php

$str = 'foobar: 2008';

preg_match('/(?<name>\w+): (?<digit>\d+/', $str, $matches);

print_r($matches);

?>  


À§ ¿¹Á¦ÀÇ Ãâ·Â:


Array
(
    [0] => foobar: 2008
    [name] => foobar
    [1] => foobar
    [digit] => 2008
    [2] => 2008
)





ÁÖÀÇ

Tip
ÇϳªÀÇ ¹®ÀÚ¿­ÀÌ ´Ù¸¥ ¹®ÀÚ¿­¿¡ µé¾îÀÖ´ÂÁö È®ÀÎÇϱâ À§Çؼ­ preg_match()¸¦ »ç¿ëÇÏÁö ¸¶½Ê½Ã¿À. ´õ¿í ºü¸¥ strpos()³ª strstr()À» »ç¿ëÇϽʽÿÀ.


Âü°í


•preg_match_all() - Àü¿ª Á¤±Ô Ç¥Çö½Ä ¸ÅÄ¡¸¦ ¼öÇàÇÕ´Ï´Ù
•preg_replace() - Á¤±Ô Ç¥Çö½Ä °Ë»ö°ú ġȯÀ» ¼öÇà
•preg_split() - Á¤±Ô Ç¥Çö½Ä¿¡ µû¶ó ¹®ÀÚ¿­À» ³ª´®
ÃßõÃßõ : 376 Ãßõ ¸ñ·Ï
¹øÈ£ Á¦¸ñ
2,645
php set time limit ÇÔ¼ö - ŸÀӾƿô ¼³Á¤
2,644
Setting .htaccess to allow PHP to be accessed with .xml extension
2,643
jQuery API: Manipulation, Events, Effects, Internals, Utilities
2,642
´Ù¿î·Îµå Ƚ¼ö Á¦ÇÑ ½ºÅ©¸³Æ®
2,641
ÀÚ¹Ù½ºÅ©¸³Æ® ŸÀÌ¸Ó - setTimeout, setInterval, clearInterval ÇÔ¼ö
2,640
jQuery AJAX ·Î±×ÀÎ ±¸Çö
2,639
PHP ¼¼¼Ç ·Î±×ÀÎ ±¸Çö
2,638
PHP ÄíÅ° ·Î±×ÀÎ ±¸Çö
2,637
jQuery¿¡¼­ json º¯¼ö »ç¿ë(È°¿ë)¹ý
2,636
AJAX ¹Ýº¹Ã³¸® ¹× ¼³¸í ¿¹Á¦
2,635
[Android] Intent È°¿ë ¿¹½Ã
2,634
[php] ¹è¿­À» ÀÓÀÇÀÇ ¼ø¼­·Î ¼¯´Â shuffle ÇÔ¼ö
2,633
jQuery API - ¼Ó¼º(CSS), ÃßÃâ
2,632
DIV ¿µ¿ªÀÇ À§Ä¡ ÁöÁ¤ÇÏ´Â ¹æ¹ý°ú ½ºÅ©·Ñ - Àý´ëÀ§Ä¡(absolute), »ó´ëÀ§Ä¡(relative), °íÁ¤À§Ä¡(fixed)
2,631
urlencode(), base64 encode() ÇÔ¼ö
2,630
PHP·Î IE¹öÀü üũÇÏ´Â ¹æ¹ý(Trident°ª»ç¿ë)
2,629
HTML5 °ü·Ã À¯¿ëÇÑ ·¹ÆÛ(ÂüÁ¶) »çÀÌÆ®
2,628
[HTML5] Video & Audio
2,627
HTML5 ¿Àµð¿À °î ¸ñ·Ï Àç»ý
2,626
HTML5 ¿Àµð¿À 404 ¿À·ù½Ã ´Ù¸¥ ÆÄÀÏ Àç»ý
2,625
HTML5 ¿Àµð¿À À̺¥Æ®¸®½º³Ê onerror
2,624
JavaScript Audio °´Ã¼
2,623
HTML5 Audio 2ä³Î Àç»ý
2,622
HTML5 Audio 2ä³Î Àç»ý + Àç»ý¿Ï·á ¸®½º³Ê
2,621
HTML5 À½¾Ç º¼·ý Á¶Àý
2,620
HTML5 À½¾Ç º¼·ý Á¶Àý
2,619
HTML5 ¿Àµð¿À ÀÚµ¿Àç»ý
2,618
HTML5 ¿Àµð¿À ¹Ýº¹ Àç»ý
2,617
HTML5 ¿Àµð¿À À̺¥Æ®¸®½º³Ê
2,616
Ŭ¸¯ ÇѹøÀ¸·Î °íÀ¯ÁÖ¼Ò(URL) º¹»çµÇ°Ô ÇÏ´Â ¹æ¹ý
¸ñ·Ï
¹ÂÁ÷Æ®·ÎÆ® ºÎ»ê±¤¿ª½Ã ºÎ»êÁø±¸ °¡¾ßµ¿ ¤Ó °³ÀÎÁ¤º¸Ãë±Þ¹æħ
Copyright ¨Ï musictrot All rights reserved.