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


preg match
10³â Àü

(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() - Á¤±Ô Ç¥Çö½Ä¿¡ µû¶ó ¹®ÀÚ¿­À» ³ª´®
ÃßõÃßõ : 509 Ãßõ ¸ñ·Ï
¹øÈ£ Á¦¸ñ
2,651
ÀÚ¹Ù½ºÅ©¸³Æ® Base64 encode, decode
2,650
PHP ¹®ÀÚ¿­ °ü·Ã ÇÔ¼ö
2,649
[php] ¹®ÀÚ¿­À» ³ª´©°Å³ª ÇÕÄ¡´Â explode, implode ÇÔ¼ö
2,648
php ¹®ÀÚ¿­ ÀÚ¸£±â ³ª´©±â ºÐ¸®Çϱâ - explode() , split()
2,647
DB Á¢±Ù ¹× Äõ¸®¹®(insert,select,update,delete)
2,646
[MySQL] Çʵ忡¼­ ƯÁ¤¹®ÀÚ Æ÷ÇÔ ¶Ç´Â Á¦¿ÜÇÑ DB °Ë»ö, LIKE ,NOT
2,645
php5 mysqli µðºñ Á¢¼Ó Ŭ·¡½º ÇÁ·Î±×·¥ ¹× »ç¿ë¹ý
2,644
PHP ¹®ÀÚ¿­ ³ª´©±â - explode()
2,643
[PHP] ¹è¿­ Array
2,642
php ¹è¿­
2,641
[php] Array() ¹è¿­À̶õ?
2,640
PHP ¹è¿­ ÇÔ¼ö Á¤¸®
2,639
PHP ±âº»ÀûÀÎ ¹è¿­ »ç¿ë¹ý
2,638
html¿¡¼­ ÀÔ·ÂÇѰª DB¿¡¼­ ¹Þ¾Æ¿À±â (µµ¼­Á¤º¸ °Ë»ö)
2,637
SQL Äõ¸® Á¤¸®
2,636
[MySql]DB¿¡¼­ ¼­·Î ´Ù¸¥ Å×À̺íÀÇ µ¥ÀÌÅ͸¦ Çѹø¿¡ ºÒ·¯¿À±â
2,635
jquery checkbox Àüü ¼±ÅÃ, Àüü ÇØÁ¦, üũ °ª ÃßÃâ
2,634
php set time limit ÇÔ¼ö - ŸÀӾƿô ¼³Á¤
2,633
Setting .htaccess to allow PHP to be accessed with .xml extension
2,632
jQuery API: Manipulation, Events, Effects, Internals, Utilities
2,631
´Ù¿î·Îµå Ƚ¼ö Á¦ÇÑ ½ºÅ©¸³Æ®
2,630
ÀÚ¹Ù½ºÅ©¸³Æ® ŸÀÌ¸Ó - setTimeout, setInterval, clearInterval ÇÔ¼ö
2,629
jQuery AJAX ·Î±×ÀÎ ±¸Çö
2,628
PHP ¼¼¼Ç ·Î±×ÀÎ ±¸Çö
2,627
PHP ÄíŰ ·Î±×ÀÎ ±¸Çö
2,626
jQuery¿¡¼­ json º¯¼ö »ç¿ë(Ȱ¿ë)¹ý
2,625
AJAX ¹Ýº¹Ã³¸® ¹× ¼³¸í ¿¹Á¦
2,624
[Android] Intent Ȱ¿ë ¿¹½Ã
2,623
[php] ¹è¿­À» ÀÓÀÇÀÇ ¼ø¼­·Î ¼¯´Â shuffle ÇÔ¼ö
2,622
jQuery API - ¼Ó¼º(CSS), ÃßÃâ
¸ñ·Ï
¹ÂÁ÷Æ®·ÎÆ® ºÎ»ê±¤¿ª½Ã ºÎ»êÁø±¸ °¡¾ßµ¿ ¤Ó °³ÀÎÁ¤º¸Ãë±Þ¹æÄ§
Copyright ¨Ï musictrot All rights reserved.