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


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 Ãßõ ¸ñ·Ï
¹øÈ£ Á¦¸ñ
283
½ÇÇàÁß PHP ¿¡·¯ ¸Þ½ÃÁö ·¹º§ Á¶Á¤Çϱâ
282
PHP - $ SERVER ȯ°æº¯¼ö
281
Example #1 print ¿¹Á¦
280
Á¤±Ô½Ä ¹®¹ý
279
ÇѱÛ, ¿µ¹®, ¼ýÀÚ, Ư¼ö¹®ÀÚ Ã¼Å©ÇÏ´Â ÇÔ¼ö
278
ÀÚÁÖ ¾²ÀÌ´Â Á¤±ÔÇ¥Çö½Ä ¸ðÀ½
277
PHP·Î Internet Explorer ¹öÀü ¾Ë¾Æ³»±â
276
PHP·Î À̹ÌÁö ÁÖ¼Ò ¼û±â±â
275
À¯Åõºê ÁÖ¼Ò¿¡¼­ ¾ÆÀ̵𸸠»Ì¾Æ¿À±â
274
preg_match_all ¸ÆÁÖ¼Ò ÃßÃâ
273
¸ÞŸÅ±׸¦ ÃßÃâÇØÁÖ´Â get_meta_tags ÇÔ¼ö
272
PHP Á¤±Ô½ÄÀ» È°¿ëÇÑ ÅÂ±× ¹× ƯÁ¤ ¹®ÀÚ¿­ Á¦°Å ¹× ÃßÃâ ¹æ¹ý
preg_match
270
À̹ÌÁö Å©±â¸¦ ºñÀ²¿¡ ¸Â°Ô Á¶ÀýÇÑ´Ù.
269
°ü°è ¿¬»êÀÚ
268
º´ÇÕ ¿¬»êÀÚ
267
SUBSTR ÇÔ¼ö
266
PHP) Example #1 HTML ¿£Æ¼Æ¼ µðÄÚµù
265
PHP Proxy °¡Á®¿À±â
264
[PHP] while($data = mysql_fetch_array($result)) ¿À·ù ³¯¶§ È®ÀÎ
263
PHP·Î À̹ÌÁö ÁÖ¼Ò ¼û±â±â
262
PHP Á¤±Ô½ÄÀ» È°¿ëÇÑ ÅÂ±× ¹× ƯÁ¤ ¹®ÀÚ¿­ Á¦°Å ¹× ÃßÃâ ¹æ¹ý
261
php °ø¹éÁ¦°Å ÇÔ¼ö Á¤¸® (¾Õ, µÚ, ¾ÕµÚ, Áß°£)
260
php Å×±×Á¦°Å ÇÔ¼ö¸ðÀ½
259
Ư¼ö¹®ÀÚ ¾ø¾Ö´Â Á¤±ÔÇ¥Çö½Ä
258
ƯÁ¤ ¹®ÀÚ¿­À» ±âÁØÀ¸·Î Áß°£¿¡ ÇÊ¿äÇÑ ¹®ÀÚ¿­ ÃßÃâÇϱâ
257
[php] ÁÖ¼®°ú °ø¹éÀ» Á¦°ÅÇÏ¿© ¹ÝȯÇÏ´Â php_strip_whitespace ÇÔ¼ö
256
for¹® break, continue
255
PHP ÇÔ¼ö¸¦ Javascript ¿¡¼­ »ç¿ëÇϱâ
254
¸µÅ© À̹ÌÁö ÀúÀå ¹æ¹ý (php)
¸ñ·Ï
¹ÂÁ÷Æ®·ÎÆ® ºÎ»ê±¤¿ª½Ã ºÎ»êÁø±¸ °¡¾ßµ¿ ¤Ó °³ÀÎÁ¤º¸Ãë±Þ¹æħ
Copyright ¨Ï musictrot All rights reserved.