"> ">

Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chungbuk HRDI of KCCI PhD Kang,Won-Chan PHP Programming (Regular Expressions)

Similar presentations


Presentation on theme: "Chungbuk HRDI of KCCI PhD Kang,Won-Chan PHP Programming (Regular Expressions)"— Presentation transcript:

1 Chungbuk HRDI of KCCI PhD Kang,Won-Chan PHP Programming (Regular Expressions)

2 PHP Programming PHP Programming - 2 - Ereg() <? $string = "[RE:8] Board posting Test"; $res = ereg("\[RE:[1-9][0-9]*\]", $string); if($res) { echo("pattern for \"$string\" : found! "); } else { echo("pattern for \"$string\" : not found! "); } $string = array("yunka0803", "informediary", "92kdkang", "JaeDoNam"); while(list($key,$val) = each($string)) { if(ereg("^[[:alpha:]][[:alnum:]]{7,9}$",$val)) { echo("pattern for \"$val\" : found! "); } else { echo("pattern for \"$val\" : not found! "); } ?>

3 PHP Programming PHP Programming - 3 - Ereg() <? $string = "2010-6-28"; if(ereg("([0-9]{4})-([0-9]{1,2})-([0-9]{1,2})", $string, $regs)) { echo("$regs[2]Mon $regs[3]day $regs[1]year "); for($i = 0; $i < sizeof($regs); $i++) { echo("\$regs[$i] : $regs[$i] "); } echo(" "); } else { echo("Wrong date type : $string "); } $string = "MidTerm - 9247167:87:B+"; $pattern = "([0-9]{7}):([0-9]{1,3}):([A-D|F][+]?)"; if(ereg($pattern,$string,$regs)) { echo("\$regs[0] : $regs[0] "); echo("\$regs[1] : $regs[1] "); echo("\$regs[2] : $regs[2] "); echo("\$regs[3] : $regs[3] "); echo("ID: $regs[1], resullt= $regs[2], final grade is $regs[3]. "); } else { echo("pattern for \"$string\" not found! "); }?>

4 PHP Programming PHP Programming - 4 - Eregi() <? $string = "TeaST 99"; $pattern = "^[A-Z]{5}"; if(ereg($pattern,$string)) { echo("pattern for \"$string\" : -found! "); } else { echo("pattern for \"$string\" : -not found! "); } if(eregi($pattern,$string)) { echo("pattern for \"$string\" : found! "); } else { echo("pattern for \"$string\" : not found! "); } ?>

5 PHP Programming PHP Programming - 5 - Ereg(): a, A is not same Eregi(): a, A is same

6 PHP Programming PHP Programming - 6 - Expression law.: 1 character s.e -> same as sae, sbe, sce… *: none or more character s*e -> same as e,se,sse,ssse…, abc*->same as ab, abc,abcc, abccc…. +: before, 1 or more character s+e->same as se,sse,ssse,settle…. ?: none or 1 character Th?e -> same as te,the…. ^: follow string ^The -> The girl… ^a?bc -> bc++ 3.0, abcdef..

7 PHP Programming PHP Programming - 7 - $: previous string A?bc$ -> eeabc, seebc, bc.. ^The$ -> the [ ]: 1 character match between[ ] [ab]cd-> acd, tacde, tbcd,… [a-z] -> a0c2ds, ta123cde, Student… {}: number of character or string{} a{2}b -> aab // 2a a{2,}b -> aab, aaab, aaaab //over 2 a a{1,3}b -> ab, aab, aaab //1~3 a ( ): make a group a(bc)* -> sea, abcd, abcbcbcbc |: or operator He|she -> he is hansome, she’s gone (le|li)*ft -> mleftto, ift, lelift,fclelelileft

8 PHP Programming PHP Programming - 8 - ereg_replace() <? $string = "By viewing these pages"; $res = ereg_replace("these", "those", $string); echo("$res"); ?>

9 PHP Programming PHP Programming - 9 - ereg_replace,”\digit” <? $string = "By viewing these pages"; $res = ereg_replace("(t[[:alpha:]]+)","that",$string); echo("$res "); $res = ereg_replace("(t[[:alpha:]]+)","\\1",$string); echo("$res "); $res = ereg_replace("t([[:alpha:]]+)","\\1",$string); echo("$res "); $res = ereg_replace("t([[:alpha:]]+)","that\\1",$string); echo("$res "); $res = ereg_replace("t([[:alpha:]]+) ([a-z]+s$)","\\1",$string); echo("$res "); $res = ereg_replace("t([[:alpha:]]+) ([a-z]+s$)","\\2",$string); echo("$res "); ?>

10 PHP Programming PHP Programming - 10 - Split() <? $string = "Kang Song Kim Choi"; $arr = split("[[:space:]]+",$string); for($i = 0; $i < sizeof($arr); $i++) { echo("\$arr[$i] : $arr[$i] "); } ?>

11 PHP Programming PHP Programming - 11 - Guest board NAME SUBJECT PASSWORD (Mixed alphabet & number 6-8character) CONTENTS

12 PHP Programming PHP Programming - 12 -

13 PHP Programming PHP Programming - 13 - Save file name is Isnull.php <? if(ereg("([^[:space:]]+)", $name)) { echo("Input name is \"${name}\". "); } else { echo("Name section is blank rewrite it.! "); exit;} if(ereg("([^[:space:]]+)", $subject)) { echo("Input subject is \"${subject}\". "); } else { echo("Subject section is blank rewrite it.! "); exit;} if(ereg("(^[0-9a-zA-Z]{6,8}$)", $passwd, $regs)) { echo("Input password is \"$regs[1]\". "); } else { echo("Password section is blank rewrite it.!! "); exit;} if(ereg("([^[:space:]]+)", $comment)) { echo("Input contents is \"${comment}\". "); } else { echo("Contentse section is blank rewrite it.! "); exit; }?>

14 PHP Programming PHP Programming - 14 - NAME SUBJECT E-MAIL PASSWORD (Mixed alphabet & nember 6-8 character) CONTENTS

15 PHP Programming PHP Programming - 15 -

16 PHP Programming PHP Programming - 16 - Save file name is validate_email.php <? if(ereg("([^[:space:]]+)", $name)) { echo("Input name is \"${name}\". "); } else { echo("Name section is blank rewrite it.! "); exit;} if(ereg("([^[:space:]]+)", $subject)) { echo("Input subject is \"${subject}\". "); } else { echo("Subject section is blank rewrite it.! "); exit;} if(ereg("(^[_0-9a-zA-Z-]+(\.[_0-9a-zA-Z-]+)*@[0-9a-zA-Z-]+(\.[0-9a-zA-Z-]+)*$)", $email, $regs)) { echo("Input E-mail is \"${comment}\". "); } else { echo("E-mail section is blank rewrite it.! "); exit;} if(ereg("(^[0-9a-zA-Z]{6,8}$)", $passwd, $regs)) { echo("Input password is \"$regs[1]\". "); } else { echo("Password section is blank rewrite it.!! "); exit;} if(ereg("([^[:space:]]+)", $comment)) { echo("Input contents is \"${comment}\". "); } else { echo("Contentse section is blank rewrite it.! "); exit; } ?>

17 PHP Programming PHP Programming - 17 - sample1 <? echo(" "); $http_url[] = "http://www.yahoo.com"; $http_url[] = "http://www.shinbiro.com/@linux"; $http_url[] = "http://myhome.netsgo.com/fathom"; $http_url[] = "http://safelab.skku.ac.kr/~younicom/"; $http_url[] = "http://safelab.skku.ac.kr/board/list.html?code= board&page_num=1"; $pattern_http = "http://([0-9a-zA-Z./@~?&=_]+)"; while(list($key, $val) = each($http_url)) { if($url = ereg_replace($pattern_http, "\\1", $val)) { echo("$url\n "); } if($url = ereg_replace($pattern_http, " http://\\1 ", $val)) { echo("$url\n "); } }

18 PHP Programming PHP Programming - 18 - sample2 $ftp_url[] = "ftp://ftp.bora.net"; $ftp_url[] = "ftp://safelab.skku.ac.kr/pub/linux"; $ftp_url[] = "ftp://localhost/linux/php_3.0.12.tar.gz"; $pattern_ftp = "ftp://([0-9a-zA-Z./@~?&=_]+)"; while(list($key, $val) = each($ftp_url)) { if($url = ereg_replace($pattern_ftp, "\\1", $val)) { echo("$url\n "); } if($url = ereg_replace($pattern_ftp, " ftp://\\1 ", $val)) { echo("$url\n "); } }

19 PHP Programming PHP Programming - 19 - sample3 $email_url[] = "yunka1018@netian.com"; $email_url[] = "9685351eng@linux.org"; $email_url[] = 92kdkang@shinbiro.com;92kdkang@shinbiro.com $pattern_email = "([_0-9a-zA-Z-]+(\.[_0-9a-zA-Z-]+)*)@([0-9a-zA-Z-]+(\.[0- 9a-zA-Z-]+)*)"; while(list($key, $val) = each($email_url)) { if($url = ereg_replace($pattern_email, "\\1@\\3", $val)) { echo("$url\n "); } if($url = ereg_replace($pattern_email, " \\1@\\3 ", $val)) { echo("$url\n "); } echo(" "); ?>

20 PHP Programming PHP Programming - 20 - - end -


Download ppt "Chungbuk HRDI of KCCI PhD Kang,Won-Chan PHP Programming (Regular Expressions)"

Similar presentations


Ads by Google