preg match (Á¤±ÔÇ¥Çö½Ä ¸ÅÄ¡¸¦ ¼öÇàÇÕ´Ï´Ù) |
|
|
 |
14³â Àü |
int preg_match ( string $pattern , string $subject [, array $matches [, int $flags [, int $offset ]]] )
pattern ¿¡ ÁÖ¾îÁø Á¤±ÔÇ¥Çö½ÄÀ» subject ¿¡¼ ã½À´Ï´Ù.
matches °¡ ÁÖ¾îÁö¸é, °Ë»ö °á°ú¸¦ ä¿ö³Ö½À´Ï´Ù. $matches[0]´Â Àüü ÆÐÅÏ ÅؽºÆ®°¡ µé¾î°¡°í, $matches[1]ºÎÅÍ °ýÈ£·Î µÑ·¯½ÎÀÎ ¼ºê ÆÐÅÏÀ» ä¿ö³Ö½À´Ï´Ù.
flags ´Â ´ÙÀ½°ú °°Àº Ç÷¡±×¸¦ »ç¿ëÇÒ ¼ö ÀÖ½À´Ï´Ù:
PREG_OFFSET_CAPTURE
ÀÌ Ç÷¡±×¸¦ ³Ñ±â¸é, ¸ðµç ¸ÅÄ¡¿¡ ´ëÇÑ ¹®ÀÚ¿ ½ÃÀÛ À§Ä¡¸¦ ÇÔ²² ¹ÝȯÇÕ´Ï´Ù. ¹Ýȯ°ªÀ» 0¿¡ ¸ÅÄ¡ÇÑ ¹®ÀÚ¿À» °¡Áö°í, 1¿¡ ¹®ÀÚ¿ ½ÃÀÛ À§Ä¡¸¦ °¡Áö´Â ¹è¿À» ¿ø¼Ò·Î °®´Â ¹è¿·Î º¯°æÇÏ´Â Á¡¿¡ ÁÖÀÇÇϽʽÿÀ. ÀÌ Ç÷¡±×´Â PHP 4.3.0ºÎÅÍ »ç¿ëÇÒ ¼ö ÀÖ½À´Ï´Ù.
flags ÀÎÀÚ´Â PHP 4.3.0ºÎÅÍ »ç¿ëÇÒ ¼ö ÀÖ½À´Ï´Ù.
º¸Åë, °Ë»öÀº ¸ñÇ¥ ¹®ÀÚ¿ÀÇ Ã³À½¿¡¼ ½ÃÀÛÇÕ´Ï´Ù. ¼±ÅÃÀûÀÎ ÀÎÀÚ offset À¸·Î °Ë»öÀ» ½ÃÀÛÇÒ ´Ù¸¥ À§Ä¡¸¦ ÁöÁ¤ÇÒ ¼ö ÀÖ½À´Ï´Ù. ÀÌ´Â preg_match()ÀÇ ¸ñÇ¥ ¹®ÀÚ¿¿¡ substr()($subject, $offset)À» ³Ñ±â´Â °Í°ú µ¿ÀÏÇÕ´Ï´Ù. offset ÀÎÀÚ´Â PHP 4.3.3ºÎÅÍ »ç¿ëÇÒ ¼ö ÀÖ½À´Ï´Ù.
preg_match()´Â pattern ÀÌ ¸ÅÄ¡µÈ Ƚ¼ö¸¦ ¹ÝȯÇÕ´Ï´Ù. ÀÌ´Â 0(¸ÅÄ¡ ¾øÀ½)À̳ª 1ÀÔ´Ï´Ù. preg_match()´Â óÀ½ ¸ÅÄ¡ ÈÄ¿¡ °Ë»öÀ» ÁßÁöÇϱ⠶§¹®ÀÔ´Ï´Ù. ´ëÁ¶ÀûÀ¸·Î, preg_match_all()´Â subject ÀÇ ³¡±îÁö °è¼ÓÇؼ ½ÇÇàÇÕ´Ï´Ù. ¿¡·¯°¡ ¹ß»ýÇϸé, preg_match()´Â FALSE¸¦ ¹ÝȯÇÕ´Ï´Ù.
Tip
´Ü¼øÈ÷ ÇϳªÀÇ ¹®ÀÚ¿ÀÌ ´Ù¸¥ ¹®ÀÚ¿¿¡ µé¾îÀÖ´ÂÁö¸¦ È®ÀÎÇÏ°í ½ÍÀ»¶§´Â preg_match()¸¦ »ç¿ëÇÏÁö ¸¶½Ê½Ã¿À. ´ë½Å, strpos()³ª strstr()¸¦ »ç¿ëÇÏ´Â ÆíÀÌ ´õ¿í ºü¸¨´Ï´Ù.
Example#1 ¹®ÀÚ¿ "php" ã±â
<?php
// ÆÐÅÏ ±¸ºÐÀÚ µÚÀÇ "i"´Â ´ë¼Ò¹®ÀÚ¸¦ ±¸º°ÇÏÁö ¾Ê°Ô ÇÕ´Ï´Ù.
if (preg_match("/php/i", "PHP is the web scripting language of choice.")) {
echo "¹ß°ßÇÏ¿´½À´Ï´Ù.";
} else {
echo "¹ß°ßÇÏÁö ¸øÇß½À´Ï´Ù.";
}
?> |
|
̵̧ : 288 |
̵̧
¸ñ·Ï
|
|