|
 |
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() - Á¤±Ô Ç¥Çö½Ä¿¡ µû¶ó ¹®ÀÚ¿À» ³ª´®
|
|
̵̧ : 445 |
̵̧
¸ñ·Ï
|
|