1. Trang chủ
  2. » Công Nghệ Thông Tin

MySQL /PHP Database Applications Second Edition phần 10 doc

88 278 0

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 88
Dung lượng 420,65 KB

Nội dung

684 Part V: Appendixes #quoted-string = <”> *(qtext/quoted-pair) <”>; Regular qtext or # ; quoted chars. # #qtext = <any CHAR excepting <”>, ; => may be folded # “\” & CR, and including # linear-white-space> # #domain-literal = “[“ *(dtext / quoted-pair) “]” # # # # #dtext = <any CHAR excluding “[“, ; => may be folded # “]”, “\” & CR, & including # linear-white-space> # #comment = “(“ *(ctext / quoted-pair / comment) “)” # #ctext = <any CHAR excluding “(“, ; => may be folded # “)”, “\” & CR, & including # linear-white-space> # #quoted-pair = “\” CHAR ; may quote any char # #phrase = 1*word ; Sequence of words # #word = atom / quoted-string # #mailbox = addr-spec ; simple address # / phrase route-addr ; name & addr-spec #route-addr = “<” [route] addr-spec “>” #route = 1#(“@” domain) “:” ; path-relative #addr-spec = local-part “@” domain ; global address #validate_email(“insight\@bedrijfsnet.nl”); function print_validate_email ($eaddr=””) { $result = validate_email($eaddr) ? “is valid” : “is not valid”; print “<h4>email address (“.htmlspecialchars($eaddr).”) $result</h4>\n”; } function validate_email ($eaddr=””) { if (empty($eaddr)) { #print “[$eaddr] is not valid\n”; return false; } $laddr = “”; $laddr = $eaddr; # if the addr-spec is in a route-addr, strip away the phrase and <>s $laddr = preg_replace(‘/^.*</’,’’, $laddr); $laddr = preg_replace(‘/>.*$/’,’’,$laddr); if (preg_match(‘/^\@.*:/’,$laddr)) #path-relative domain { list($domain,$addr_spec) = preg_split(‘/:/’,$laddr); $domain = preg_replace(‘/^\@/’,’’,$domain); if (!is_domain($domain)) { return false; } $laddr = $addr_spec; } return(is_addr_spec($laddr)); } function is_addr_spec ( $eaddr = “” ) { list($local_part,$domain) = preg_split(‘/\@/’,$eaddr); if (!is_local_part($local_part) || !is_domain($domain)) { #print “[$eaddr] is not valid\n”; return false; } else { #print “[$eaddr] is valid\n”; return true; } } #local-part = word *(“.” word) ; uninterpreted function is_local_part ( $local_part = “” ) { if (empty($local_part)) { return false; } $bit_array = preg_split(‘/\./’,$local_part); Appendix H: Helpful User-Defined Functions 685 686 Part V: Appendixes while (list(,$bit) = each($bit_array)) { if (!is_word($bit)) { return false; } } return true; } #word = atom / quoted-string #quoted-string = <”> *(qtext/quoted-pair) <”>; Regular qtext or # ; quoted chars. #qtext = <any CHAR excepting <”>, ; => may be folded # “\” & CR, and including # linear-white-space> #quoted-pair = “\” CHAR ; may quote any char function is_word ( $word = “”) { if (preg_match(‘/^”.*”$/i’,$word)) { return(is_quoted_string($word)); } return(is_atom($word)); } function is_quoted_string ( $word = “”) { $word = preg_replace(‘/^”/’,’’,$word); # remove leading quote $word = preg_replace(‘/”$/’,’’,$word); # remove trailing quote $word = preg_replace(‘/\\+/’,’’,$word); # remove any quoted- pairs if (preg_match(‘/\”\\\r/’,$word)) # if “, \ or CR, it’s bad qtext { return false; } return true; } #atom = 1*<any CHAR except specials, SPACE and CTLs> #specials = “(“ / “)” / “<” / “>” / “@” ; Must be in quoted- # / “,” / “;” / “:” / “\” / <”> ; string, to use # / “.” / “[“ / “]” ; within a word. #SPACE = <ASCII SP, space> ; ( 40, 32.) #CTL = <any ASCII control ; ( 0- 37, 0 31.) # character and DEL> ; ( 177, 127.) function is_atom ( $atom = “”) { if ( (preg_match(‘/[\(\)\<\>\@\,\;\:\\\”\.\[\]]/’,$atom)) # specials || (preg_match(‘/\040/’,$atom)) # SPACE || (preg_match(‘/[\x00-\x1F]/’,$atom)) # CTLs ) { return false; } return true; } #domain = sub-domain *(“.” sub-domain) #sub-domain = domain-ref / domain-literal #domain-ref = atom ; symbolic reference function is_domain ( $domain = “”) { if (empty($domain)) { return false; } # this is not strictly required, but is 99% likely sign of a bad domain if (!preg_match(‘/\./’,$domain)) { return false; } $dbit_array = preg_split(‘/./’,$domain); while (list(,$dbit) = each($dbit_array)) { if (!is_sub_domain($dbit)) { return false; } } return true; } function is_sub_domain ( $subd = “”) { if (preg_match(‘/^\[.*\]$/’,$subd)) #domain-literal { return(is_domain_literal($subd)); } return(is_atom($subd)); } #domain-literal = “[“ *(dtext / quoted-pair) “]” Appendix H: Helpful User-Defined Functions 687 688 Part V: Appendixes #dtext = <any CHAR excluding “[“, ; => may be folded # “]”, “\” & CR, & including # linear-white-space> #quoted-pair = “\” CHAR ; may quote any char function is_domain_literal ( $dom = “”) { $dom = preg_replace(‘/\\+/’,’’,$dom); # remove quoted pairs if (preg_match(‘/[\[\]\\\r]/’,$dom)) # bad dtext characters { return false; } return true; } ?> You would probably want to put all of these functions in one file and then include it when needed. It returns 1 (for true) or nothing (for false). You’d probably want to use it like so: if ( !validate_email(“myaddress@mydomain.com”) ) { echo “this is not a valid email”; } sitemap.php We wrote the following code to help us take a look at all the documents installed on the Web server. It prints every document and provides a link to these documents. <?php include(‘book.php’); ?> <b><a href=”.”>back to directory</a></b> <h2>Site Map</h2> <?php function printdir($dir=’.’,$path=NULL, $print_ok=FALSE) { if ($path === NULL) $path = $dir; $pursue = TRUE; $old_print_ok = $print_ok; if ($print_ok) { // np } elseif (strpos($path, BOOK_ROOT) === FALSE) { if (strpos($path, DSN_ROOT) === FALSE) { if (strpos(BOOK_ROOT, $path) === FALSE && strpos(DSN_ROOT, $path) === FALSE ) { $pursue = FALSE; } } else { $print_ok = TRUE; } } else { $print_ok = TRUE; } $printdir = $dir; if ($print_ok && !$old_print_ok) $printdir = $path; $url = NULL; if (strpos($path, DOC_ROOT) !== FALSE) { $url = str_replace(DOC_ROOT, ‘’, $path); $printdir = “<a href=\”$url\”>$printdir</a>”; } if ($dh = opendir($path)) { if ($print_ok) print “<li>$printdir/\n<ul>\n”; while (($file = readdir($dh)) !== FALSE) { if ($file == ‘.’ or $file == ‘ ’ or $file == ‘CVS’ or substr($file,-4) == ‘.swp’) continue; $wholefile = “{$path}/{$file}”; if ($url) if (substr($file,-4) == ‘.php’) $printfile = ‘<a href=”’ . BOOK_URL_ROOT Appendix H: Helpful User-Defined Functions 689 690 Part V: Appendixes . ‘/source’ . str_replace(BOOK_URL_ROOT, ‘/book/’, “{$url}/{$file}”) . ‘“>’ . $file . ‘</a>’ ; else $printfile = “<a href=\”{$url}/{$file}\”>$file</a>”; else $printfile = $file; if (is_link($wholefile)) { if ($print_ok) print “<li>@{$printfile}\n”; } elseif (is_dir($wholefile)) { if ($pursue) printdir($file, $wholefile, $print_ok); } elseif ($print_ok) { print “<li>$printfile\n”; } } closedir($dh); if ($print_ok) print “</ul>\n”; } else { print “<li>could not open ‘$dir’\n”; } } printdir(‘/usr/local/book/apache’); ?> Appendix I PHP and MySQL Resources THIS APPENDIX PRESENTS SOME resources that should be extremely useful in increas- ing your knowledge of both PHP and MySQL. PHP Resources Here are some sites that are great for all things PHP. php.net This site, along with its many international mirrors, should be your home away from home. From the home page, you can search the manual or one of the many mailing lists. Among the many helpful resources are the following: ◆ PHP Annotated Manual (http://www.php.net/manual/) — The online manual is really terrific; it includes user comments, some of which clarify the use of some of the trickier functions in PHP. ◆ Downloads (http://www.php.net/downloads.php) — Here you can find not only the various distributions, but also an HTML manual that you can download and put on your local machine. ◆ Daily snapshots (http://snaps.php.net) — PHP is an active open-source project, and features and bug fixes are constantly added to the code base. Before official releases are made, you can get the most up-to-date code here: Source code is updated daily. Note that this service is best for the true hacker with a box devoted to development; if you have room for only one installation, get the most recent stable source code. A link to the most recent stable source is always available from http://www.php.net/. ◆ Bug database (http://bugs.php.net) — Wondering if there is a problem with a function? Head over to this site to search through the bug reports, or to add one yourself — but be very sure that you’ve found a bug before submitting a report. ◆ FAQ (http://www.php.net/FAQ.php) — Before you post to any mailing list or start writing an application, read the FAQ. 691 PHP mailing lists One of the great things about the Web, and about open-source projects in particu- lar, is the quality of the advice available on the mailing lists. Many lists, covering many specific topics, are available. The ones discussed in this section are all part of php.net and use the lists.php.net mail domain. You can subscribe to any of these lists on http://www.php.net/mailing-lists.php, and they are all archived at http://marc.theaimsgroup.com/. The core developers monitor the list and respond to questions and complaints. If you want to keep up with the goings-on of any of the lists but would rather not stuff up your inbox, you can also get to these mailing lists via a newsgroup reader. Just connect to news.php.net. ◆ PHP general — This is the generic support area. Over the course of a typi- cal day over 100 emails are posted to this list. It is amazingly helpful, even if you don’t have an interest in posting questions or supplying answers. Your comrades have some interesting techniques and knowledge, which they share daily. Please practice good etiquette when posting to the mailing lists. First check one of the searchable archives to make sure your question is something resembling unique. And please, read the FAQ. ◆ Database list — This one is a natural for most everyone reading this book because it has to do with how PHP interacts with databases. This is key to almost all Web applications. ◆ Installation list — If you are having problems getting PHP installed on your box, this is the place to go. zend.com At the core of the PHP is the Zend engine, which was built by Zeev Suraski and Andi Gutmans. Their work became the foundation for a company that is offering products that make PHP even more powerful. Zend products include a cache, which can really increase speed, an optimizer, which can help make badly written code 692 Part V: Appendixes run faster, a compiler, which makes PHP unreadable (which is great if you’re plan- ning on distributing code that you would rather not be open source), and an inte- grated development environment (IDE). And who wouldn’t want that? The zend.com site includes some valuable resources: ◆ Code Gallery (http://zend.com/codex.php) — This is one of the better code galleries out there. Browse it and see if it contains functions that will make your life easier. ◆ Applications (http://zend.com/apps.php) — What? What you have here isn’t enough? ◆ Tutorials (http://zend.com/zend/tut/) — Zend provides a growing number of very informative tutorials that cover a variety of topics. ◆ Weekly Summary (http://zend.com/zend/week/) — Avi Lewin writes a weekly article that summarizes the major issues the core developers dis- cussed over the past week. It’s interesting stuff, and can give you a heads- up about what will be happening in PHP’s future. phpbuilder.com PHPBuilder was once the best source for PHP articles. Tim Perdue, who used to run PHPBuilder, built a great base of articles that cover topics including databases, Cascading Style Sheets, and other topics of interest to developers who work in the Web environment. PHPBuilder also has discussion boards, job boards, and a code library. It is really worth checking with frequently, although the quality has dropped off over the past year or so. phpMyAdmin on Sourceforge Earlier in the book we recommended the phpMyAdmin, a PHP tool for Web-based administration of MySQL. Tobias Ratschiller and Till Gerken provide several other useful tools. It’s all on SourceForge at http://sourceforge.net/projects/ phpmyadmin/ . PEAR PEAR stands for the PHP Extension and Application Repository, and we’ve men- tioned it many times already in this book. It is a set of code being written by some very skilled programmers whose goal is a common set of well-written extensions the rest of us can incorporate into our own PHP applications. The extensions include a templating engine, a database-abstraction layer, and much much more. Stig Bakken, one of the core developers, is heading up the project. Appendix I: PHP and MySQL Resources 693 [...]... SIGN(X)RETURNS: intmysql> select sign (10) , sign( -10) , sign(0); + + -+ -+ | sign (10) | sign( -10) | sign(0) | + + -+ -+ | 1 | -1 | 0 | + + -+ -+ 1 row in set (0.00 sec) Appendix J: MySQL Function Reference MOD Modulo is like the % operator in C It returns the remainder of N divided by M: MOD(N,M) or N % M RETURNS: int mysql> select mod (10, 3), mod (10, 4); + -+ -+ | mod (10, 3)... its documentation, you will see that it really isn’t very difficult to work with Appendix J MySQL Function Reference MYSQL HAS MANY FUNCTIONS, and only some of these were used in the course of this book You should, however, have a good idea of what other MySQL functions are available, as you might find they come in handy at times To see the complete MySQL function reference check out MySQL AB’s documentation... http://hotwired.lycos.com/webmonkey/programming/php/ Appendix I: PHP and MySQL Resources MySQL Resources There’s no shortage of resources here either We’ve mentioned mainly Web-based resources in this appendix; however, we must mention one hard-copy MySQL resource Jay Greenspan, the co-author of this book, also wrote MySQL Weekend Crash Course (Wiley 2002) mysql. com Predictably, this is probably the... sec) MATCH AGAINST Starting in MySQL version 3.23, MySQL incorporates full-text searching Using full-text searching you test the relevance of given rows against a string pattern We didn’t use full-text searching in the applications in this book, but if you’re interested in this feature we recommend reading Section 6.8 of the MySQL manual, available at http://www .mysql. com /doc/ F/u/Fulltext_Search.html... be able to use when working with MySQL Of these, the GUI clients are particularly interesting ◆ Documentation (http://www .mysql. com/documentation/) — The online manual for MySQL is pretty good and available in several languages It covers many topics that this book did not For example, the manual’s language reference should be bookmarked on your browser Both PHP and MySQL have downloadable HTML manuals... number It is equivalent to CONV(N ,10, 8) It returns NULL if N is NULL OCT(N) RETURNS: string Appendix J: MySQL Function Reference HEX This function returns a string representation of the hexadecimal value of N, where N is a long (BIGINT) number This is equivalent to CONV(N ,10, 16) It returns NULL if N is NULL HEX(N) RETURNS: string mysql> select hex (100 0); + -+ | hex (100 0) | + -+ | 3E8 | + -+... RETURNS: string LEFT This function returns the leftmost len characters from the string str: LEFT(str,len) RETURNS: string 713 714 Part V: Appendixes mysql> select left( mysql functions’, 10) ; + -+ | left( mysql functions’, 10) | + -+ | mysql func | + -+ 1 row in set (0.02 sec) RIGHT This function returns the rightmost len characters from the string str: RIGHT(str,len)... the right of Appendix J: MySQL Function Reference the final delimiter (counting from the right) is returned If count is 0, nothing is returned SUBSTRING_INDEX(str,delim,count) (used in examples) RETURNS: string mysql> select substring_index(‘mysqlfunctionsmysql’, ‘fu’, 1); + -+ | substring_index(‘mysqlfunctions’, ‘fu’, 1) | + -+ | mysql | + ... substring_index(‘mysqlfunctions’, ‘fu’, 1) | + -+ | mysql | + -+ 1 row in set (0.00 sec) mysql> select substring_index(‘mysqlfunctionsmysql’, ‘fu’, -1); + + | substring_index(‘mysqlfunctionsmysql’, ‘fu’, -1) | + + | nctionsmysql | + + 1 row in set (0.00 sec) LTRIM This function returns the string str with leading... natural logarithms) raised to the power of X: EXP(X) RETURNS: float Appendix J: MySQL Function Reference LOG This function returns the natural logarithm of X If you want the log of a number X to some arbitrary base B, use the formula LOG(X)/LOG(B) LOG(X) RETURNS: float LOG10 LOG10 returns the base -10 logarithm of X: LOG10(X) RETURNS: float POW This function returns the value of X raised to the power . other MySQL functions are available, as you might find they come in handy at times. To see the complete MySQL function reference check out MySQL AB’s documentation at http:// www .mysql. com/documentation /mysql/ bychapter/manual_Reference.html . String. whether X is negative, 0, or positive: SIGN(X)RETURNS: intmysql> select sign (10) , sign( -10) , sign(0); + + + + | sign (10) | sign( -10) | sign(0) | + + + + | 1 | -1 | 0 | + + + + 1 row in set. the remainder of N divided by M: MOD(N,M) or N % M RETURNS: int mysql& gt; select mod (10, 3), mod (10, 4); + + + | mod (10, 3) | mod (10, 4) | + + + | 1 | 2 | + + + 1 row in set (0.05 sec) FLOOR This

Ngày đăng: 12/08/2014, 21:20

TỪ KHÓA LIÊN QUAN