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

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

Embed Size (px)

Citation preview

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

Chungbuk HRDI of KCCIPhD Kang,Won-Chan

PHP Programming(Regular Expressions)

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

PHP ProgrammingPHP Programming

- - 22 - -

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

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

PHP ProgrammingPHP Programming

- - 33 - -

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<p>"); for($i = 0; $i < sizeof($regs); $i++) { echo("\$regs[$i] : $regs[$i]<br>"); } echo("<p>");} else { echo("Wrong date type : $string<p>");}$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]<br>"); echo("\$regs[1] : $regs[1]<br>"); echo("\$regs[2] : $regs[2]<br>"); echo("\$regs[3] : $regs[3]<p>"); echo("ID: $regs[1], resullt= $regs[2], final grade is $regs[3].<p>");} else { echo("pattern for \"$string\" not found!<br>");}?>

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

PHP ProgrammingPHP Programming

- - 44 - -

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

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

PHP ProgrammingPHP Programming

- - 55 - -

Ereg(): a, A is not sameEregi(): a, A is same

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

PHP ProgrammingPHP Programming

- - 66 - -

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 characters+e->same as se,sse,ssse,settle….

?: none or 1 characterTh?e -> same as te,the….

^: follow string^The -> The girl…^a?bc -> bc++ 3.0, abcdef..

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

PHP ProgrammingPHP Programming

- - 77 - -

$: 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

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

PHP ProgrammingPHP Programming

- - 88 - -

ereg_replace()<?$string = "By viewing these pages";

$res = ereg_replace("these", "those", $string);echo("$res");?>

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

PHP ProgrammingPHP Programming

- - 99 - -

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

$string);echo("$res<br>");$res = ereg_replace("t([[:alpha:]]+) ([a-z]+s$)","\\2",

$string);echo("$res<p>");?>

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

PHP ProgrammingPHP Programming

- - 1010 - -

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

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

PHP ProgrammingPHP Programming

- - 1111 - -

Guest board<html><body><form method="POST" action="isnull.php"><table width="602" border="0" cellspacing="0" cellpadding="1" align="center"><tr> <td bgColor="#8080FF"> <table width="600" border="0" cellspacing="1" cellpadding="5" align="center"> <tr> <td width="100" align="left" bgColor="#CFD0ED"><font size=2>NAME</font></td> <td width="500" bgColor="#FAFAEE"><input type="text" name="name" size="20" maxlength="10"></td> </tr> <tr> <td width="100" align="left" bgColor="#CFD0ED"><font size=2>SUBJECT</font></td> <td width="500" bgColor="#FAFAEE"><input type="text" name="subject" size="30" maxlength="40"></td> </tr> <tr> <td width="100" align="left" bgColor="#CFD0ED"><font size=2>PASSWORD</font></td> <td width="500" bgColor="#FAFAEE"><input type="password" name="passwd" size="10"> <font

size="2">(Mixed alphabet & number 6-8character)</font></td> </tr> <tr> <td width="100" align="left" bgColor="#CFD0ED"><font size=2>CONTENTS</font></td> <td width="500" bgColor="#FAFAEE"><textarea name="comment" cols="50" rows="10"></textarea> </tr> <tr> <td align="center" colspan="2" bgColor="#CFD0ED"> <font size=2> <input type="submit" value="WRITE"> <input type="reset" value="CANCEL"> </font> </td> </tr> </table> </td></tr></table></form></body></html>

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

PHP ProgrammingPHP Programming

- - 1212 - -

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

PHP ProgrammingPHP Programming

- - 1313 - -

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

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

PHP ProgrammingPHP Programming

- - 1414 - -

<html><body>

<form method="POST" action="validate_email.php"><table width="602" border="0" cellspacing="0" cellpadding="1" align="center"><tr> <td bgColor="#8080FF"> <table width="600" border="0" cellspacing="1" cellpadding="5" align="center"> <tr> <td width="100" align="left" bgColor="#CFD0ED"><font size=2>NAME</font></td> <td width="500" bgColor="#FAFAEE"><input type="text" name="name" size="20" maxlength="10"></td> </tr> <tr> <td width="100" align="left" bgColor="#CFD0ED"><font size=2>SUBJECT</font></td> <td width="500" bgColor="#FAFAEE"><input type="text" name="subject" size="30" maxlength="40"></td> </tr> <tr>

<td width="100" align="left" bgColor="#CFD0ED"><font size=2>E-MAIL</font></td> <td width="500" bgColor="#FAFAEE"><input type="text" name="email" size="30"

maxlength="40"></td> </tr> <tr> <td width="100" align="left" bgColor="#CFD0ED"><font size=2>PASSWORD</font></td> <td width="500" bgColor="#FAFAEE"><input type="password" name="passwd" size="10"> <font size="2">(Mixed

alphabet & nember 6-8 character)</font></td> </tr> <tr> <td width="100" align="left" bgColor="#CFD0ED"><font size=2>CONTENTS</font></td> <td width="500" bgColor="#FAFAEE"><textarea name="comment" cols="50" rows="10"></textarea> </tr> <tr> <td align="center" colspan="2" bgColor="#CFD0ED"> <font size=2> <input type="submit" value="WRITE"> <input type="reset" value="CANCEL"> </font> </td> </tr> </table> </td></tr></table></form></body></html>

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

PHP ProgrammingPHP Programming

- - 1515 - -

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

PHP ProgrammingPHP Programming

- - 1616 - -

Save file name is

validate_email.php<?if(ereg("([^[:space:]]+)", $name)) { echo("Input name is \"${name}\".<p>");} else { echo("Name section is blank rewrite it.!<p>"); exit;}if(ereg("([^[:space:]]+)", $subject)) { echo("Input subject is \"${subject}\".<p>");} else { echo("Subject section is blank rewrite it.!<p>"); 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}\".<p>");} else { echo("E-mail section is blank rewrite it.!<p>"); exit;}if(ereg("(^[0-9a-zA-Z]{6,8}$)", $passwd, $regs)) { echo("Input password is \"$regs[1]\".<p>");} else { echo("Password section is blank rewrite it.!!<p>"); exit;}if(ereg("([^[:space:]]+)", $comment)) { echo("Input contents is \"${comment}\".<p>");} else { echo("Contentse section is blank rewrite it.!<p>"); exit;}?>

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

PHP ProgrammingPHP Programming

- - 1717 - -

sample1<?echo("<font size=\"2\">");$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<br>"); } if($url = ereg_replace($pattern_http, "<a href=\"http://\\

1\">http://\\1</a>", $val)) { echo("$url\n<p>"); } }

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

PHP ProgrammingPHP Programming

- - 1818 - -

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<br>"); } if($url = ereg_replace($pattern_ftp, "<a href=\"ftp://\\

1\">ftp://\\1</a>", $val)) { echo("$url\n<p>"); } }

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

PHP ProgrammingPHP Programming

- - 1919 - -

sample3$email_url[] = "[email protected]";$email_url[] = "[email protected]";$email_url[] = [email protected];$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<br>"); } if($url = ereg_replace($pattern_email, "<a href=\"mailto:\\1\

@\\3\">\\1@\\3</a>", $val)) { echo("$url\n<p>"); } }echo("</font>");?>

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

PHP ProgrammingPHP Programming

- - 2020 - -

- end -