|
 |
11³â Àü |
preg_matchÀÇ patternÀÎÀÚ¿¡¼ modifier(±¸ºÐÀÚ?)º° ÀǹÌ
i : ´ë¼Ò¹®ÀÚ ±¸ºÐ¾ÈÇÔ
u : utf-8(ÀÚ¼¼ÇÑ »çÇ×Àº È®ÀÎ Áß)
utf-8¿¡¼ ¸ðµç¹®ÀÚ¸¦ °¢°¢ÀÇ ¹®ÀÚº°·Î ÀÚ¸£±â
¿¹Á¦)
<?php
$str = 'ÇÑ±Û english ªÉªò«¦«£ ñéÏÐ £££¦£ª¡×¡Ø¡Ù¡Ú';
preg_match_all('/./u', $str, $match);
echo implode(',', $match[1]);
?>
°á°ú°ª)
ÇÑ,±Û, ,e,n,g,l,i,s,h, ,ªÉ,ªò,«¦,«£, ,ñé,ÏÐ, ,££,£¦,£ª,¡×,¡Ø,¡Ù,¡Ú
preg_match
(PHP 4, PHP 5)
preg_match -- Á¤±ÔÇ¥Çö½Ä ¸ÅÄ¡¸¦ ¼öÇàÇÕ´Ï´Ù.
¼³¸í
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¸¦ ¹ÝȯÇÕ´Ï´Ù.
ÀÛÀº Á¤º¸
´Ü¼øÈ÷ ÇϳªÀÇ ¹®ÀÚ¿ÀÌ ´Ù¸¥ ¹®ÀÚ¿¿¡ µé¾îÀÖ´ÂÁö¸¦ È®ÀÎÇÏ°í ½ÍÀ»¶§´Â preg_match()¸¦ »ç¿ëÇÏÁö ¸¶½Ê½Ã¿À. ´ë½Å, strpos()³ª strstr()¸¦ »ç¿ëÇÏ´Â ÆíÀÌ ´õ¿í ºü¸¨´Ï´Ù.
¿¹ 1620. ¹®ÀÚ¿ "php" ã±â
<?php
// ÆÐÅÏ ±¸ºÐÀÚ µÚÀÇ "i"´Â ´ë¼Ò¹®ÀÚ¸¦ ±¸º°ÇÏÁö ¾Ê°Ô ÇÕ´Ï´Ù.
if (preg_match("/php/i", "PHP is the web scripting language of choice.")) {
echo "¹ß°ßÇÏ¿´½À´Ï´Ù.";
} else {
echo "¹ß°ßÇÏÁö ¸øÇß½À´Ï´Ù.";
}
?>
¿¹ 1621. ´Ü¾î "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 "¹ß°ßÇÏÁö ¸øÇß½À´Ï´Ù.";
}
?>
¿¹ 1622. URL¿¡¼ µµ¸ÞÀÎ À̸§ ¾ò±â
<?php
// URL¿¡¼ È£½ºÆ® À̸§ ¾ò±â
preg_match("/^(http:\/\/)?([^\/]+)/i",
"http://www.php.net/index.html", $matches);
$host = $matches[2];
// È£½ºÆ® À̸§¿¡¼ ¸¶Áö¸· µÎ ¼¼±×¸àÆ® ¾ò±â
preg_match("/[^\.\/]+\.[^\.\/]+$/", $host, $matches);
echo "µµ¸ÞÀÎ À̸§Àº: {$matches[0]}\n";
?>
ÀÌ ¿¹Á¦ÀÇ °á°ú:
µµ¸ÞÀÎ À̸§Àº: php.ne
|
|
̵̧ : 507 |
̵̧
¸ñ·Ï
|
|