PHP 5.3.0 ¿¡¼ »ç¶óÁø ÇÔ¼öµé ´ëü |
|
|
![](icon/private_name/1.gif) |
7³â Àü |
PHP 5.3.0 ¹öÀü À̻󿡼 »ç¶óÁ®¹ö¸° ºñ¿îÀÇ ÇÔ¼öµéÀÌ ÀÖ½À´Ï´Ù. ¹Ù·Î POSIX Regex ÇÔ¼öµéÀÔ´Ï´Ù. ´ëºÎºÐ ¸¹ÀÌµé ½á¿Ô´ø ÇÔ¼öÀ̱⿡, Àúµµ ±×·¸°í ¾ÆÁ÷±îÁö ¸¹ÀººÐµéÀÌ 5.3.0 ÀÌ»ó ¹öÀüÀ¸·Î ¾÷µ¥ÀÌÆ®¸¦ ÇÏÁö ¸øÇÏ°í ÀÖÀ» µí ÇÕ´Ï´Ù.
ÇÏÁö¸¸, PHP 6.0.0 ÀÌ»ó¹öÀüºÎÅÍ ¿ÏÀüÈ÷ »ç¶óÁú ÀÌ ÇÔ¼öµéÀ» ¾ðÁ¦±îÁö°í ²ø°í°¥ ¼ö ¾ø±â¶§¹®¿¡ ´ëüÇÔ¼ö(?)·Î ¹Ù²Ù´Â ¹æ¹ýÀ» °£´ÜÇÏ°Ô ¼³¸íÇÒ·Á°í ÇÕ´Ï´Ù.
splitÇÔ¼ö
splitÀº ƯÁ¤ÆÐÅÏÀ¸·Î ¹®ÀÚ¿À» À߶óÁÖ´Â ÇÔ¼öÀÔ´Ï´Ù.
¿¹¸¦ µé¾î, $arr = split(',', '¾Æºü,¾ö¸¶'); ¶ó°í ÇÑ´Ù¸é $arr ¿¡´Â Array('¾Æºü','¾ö¸¶'); °¡ µÇ´Â °ÍÀÌÁö¿ä.
ÀÌÇÔ¼ö´Â 5.3.0 ÀÌ»ó¹öÀüºÎÅÍ explodeÇÔ¼ö·Î ´ëüµÇ¾î¾ß ÇÕ´Ï´Ù. »ç¿ë¹æ¹ýÀº ¶È°°½À´Ï´Ù¸¸, splitÇÔ¼ö°¡ Á¤±Ô½ÄÀ» Áö¿øÇß´Ù¸é explode´Â ÇÏÁö ¾Ê´Â´Ù¶ó´Â °ÍÀÌ ´Ù¸¨´Ï´Ù.
eregÇÔ¼ö
ereg°è¿ÀÇ ÇÔ¼ö´Â Á¤±Ô½ÄÀ¸·Î ¹®ÀÚ¿À» ã°Å³ª, ãÀº¹®ÀÚ¿À» ¹Ù²Ù°Å³ª ÇÏ´Â ÇÔ¼öÀÔ´Ï´Ù.
ereg_replace($patten, $replacement, $string); ÇüÅ·Π¾²ÀÔ´Ï´Ù.
ÀÌ ÇÔ¼ö´Â 5.3.0 ÀÌ»óºÎÅÍ preg_replace·Î ¹Ù²Ù¾î¾ßÇÕ´Ï´Ù. µÎ°³ÀÇ ÇÔ¼öÀÇ ÀÎÀÚ¼ø¼´Â °°Áö¸¸, eregÇÔ¼ö¿Í pregÇÔ¼ö´Â Á¤±Ô½ÄÀ» Ç¥ÇöÇÏ´Â ¹æ¹ýÀÌ ¾à°£ Ʋ¸³´Ï´Ù.
$str = ereg_replace('[a-z]+', 'A', $str);
eregÇÔ¼ö°¡ À§¿Í °°ÀÌ Á¤±Ô½ÄÀ» Àû¾ú´Ù¸é, pregÇÔ¼ö´Â ¾Æ·¡¿Í °°ÀÌ Àû½À´Ï´Ù.
$str = preg_replace('/[a-z]+/', 'A', $str);
±×¸®°í ereg°è¿ÀÌ ´ë¼Ò¹®ÀÚ¸¦ ±¸ºÐÇÏ´Â °Í°ú ±¸ºÐÇÏÁö ¾Ê´Â °ÍÀÌ ereg, eregi µÎ°¡Áö ÇÔ¼ö·Î ±¸ºÐµÇ¾ú´Ù¸é, preg´Â ÇϳªÀÇ ÇÔ¼ö·Î 󸮵˴ϴÙ. ¿¹¸¦ µé¾î À§ÀÇ preg_replace ÇÔ¼öÀÇ Á¤±Ô½Ä¿¡¼ ´ë¼Ò¹®ÀÚ¸¦ ±¸ºÐÇÏÁö ¾Ê°Ú´Ù°í ÇÑ´Ù¸é, ¾Æ·¡¿Í °°ÀÌ i°¡ Ãß°¡µË´Ï´Ù.
$str = preg_replace('/[a-z]+/i', 'A', $str);
±× ¿Ü¿¡ ¹®ÀÚ¿À» °Ë»öÇÏ´ø eregÇÔ¼ö´Â preg_matchÇÔ¼ö·Î ´ëüµÇ¾î¾ß ÇÕ´Ï´Ù.
$str = 'abcedf';
if (ereg('[abc]'. $str, $matches) == true) print_r($matches);
¹¹ Àú·±½ÄÀ¸·Î eregÇÔ¼ö¸¦ »ç¿ëÇϵíÀÌ preg_matchÇÔ¼öµµ »ç¿ëÇÏ¸é µÇÁö¸¸ Á¤±Ô½Ä ÀԷºκÐÀº ¿ª½Ã ¾à°£ ´Ù¸¨´Ï´Ù.
$str = 'abc123abc';
if (preg_match('/([a-z]+)([0-9]+)/', $str, $matches) == true) print_r($matches);
À§ÀÇ Ã³·³ 3¹ø° ÀÎÀÚ¿¡ °Ë»öµÈ ÆÐÅÏÀÌ ¹è¿·Î ´ã±â´Â ÀÎÀÚ¸¦ ³Ö½À´Ï´Ù.
preg_match°¡ true°¡ µÈ´Ù¸é, $matches¾È¿¡´Â ÀÏÄ¡µÈ ¹®ÀÚ¿ÀÌ ¹è¿·Î µé¾î°¡°Ô µÇÁÒ. Á¤±Ô½Ä¿¡ ( ) °¡ ¾ø´Ù¸é $matches[0]¿¡ ÀÏÄ¡µÈ ÀüüÆÐÅÏÀÌ µé¾î°¡°Ô µÇ°Ú°í, ( )°¡ ÀÖ´Ù¸é $matches[0] ÀÌÈÄÀÇ $matches[1]ºÎÅÍ ( ) ¾ÈÀÇ ÀÏÄ¡ºÎºÐ¸¸ »©¿Ã ¼ö ÀÖ½À´Ï´Ù.
|
|
̵̧ : 356 |
̵̧
¸ñ·Ï
|
|