Copyright © 2003 Pearson Education, Inc. Slide 6a-1 The Web Wizards Guide to PHP by David Lash.

Slides:



Advertisements
Similar presentations
Numbers Treasure Hunt Following each question, click on the answer. If correct, the next page will load with a graphic first – these can be used to check.
Advertisements

TK1924 Program Design & Problem Solving Session 2011/2012
3rd Annual Plex/2E Worldwide Users Conference 13A Batch Processing in 2E Jeffrey A. Welsh, STAR BASE Consulting, Inc. September 20, 2007.
Copyright © 2003 Pearson Education, Inc. Slide 7-1 The Web Wizards Guide to PHP by David Lash.
AP STUDY SESSION 2.
1
Copyright © 2003 Pearson Education, Inc. Slide 6b-1 The Web Wizards Guide to PHP by David Lash.
Copyright © 2003 Pearson Education, Inc. Slide 5-1 The Web Wizards Guide to PHP by David Lash.
1 Copyright © 2002 Pearson Education, Inc.. 2 Chapter 2 Getting Started.
Copyright © 2003 Pearson Education, Inc. Slide 1.
Copyright © 2003 Pearson Education, Inc. Slide 1 Computer Systems Organization & Architecture Chapters 8-12 John D. Carpinelli.
Copyright © 2003 Pearson Education, Inc. Slide 7-1 Created by Cheryl M. Hughes The Web Wizards Guide to XML by Cheryl M. Hughes.
McGraw-Hill/Irwin Copyright © 2013 by The McGraw-Hill Companies, Inc. All rights reserved. Extended Learning Module D (Office 2007 Version) Decision Analysis.
Copyright © 2011, Elsevier Inc. All rights reserved. Chapter 6 Author: Julia Richards and R. Scott Hawley.
Author: Julia Richards and R. Scott Hawley
1 Copyright © 2013 Elsevier Inc. All rights reserved. Appendix 01.
1 Hyades Command Routing Message flow and data translation.
© 2010 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of Chapter 5: Repetition and Loop Statements Problem Solving & Program.
1 RA I Sub-Regional Training Seminar on CLIMAT&CLIMAT TEMP Reporting Casablanca, Morocco, 20 – 22 December 2005 Status of observing programmes in RA I.
Local Customization Chapter 2. Local Customization 2-2 Objectives Customization Considerations Types of Data Elements Location for Locally Defined Data.
Process a Customer Chapter 2. Process a Customer 2-2 Objectives Understand what defines a Customer Learn how to check for an existing Customer Learn how.
Custom Statutory Programs Chapter 3. Customary Statutory Programs and Titles 3-2 Objectives Add Local Statutory Programs Create Customer Application For.
Custom Services and Training Provider Details Chapter 4.
1 Click here to End Presentation Software: Installation and Updates Internet Download CD release NACIS Updates.
Photo Slideshow Instructions (delete before presenting or this page will show when slideshow loops) 1.Set PowerPoint to work in Outline. View/Normal click.
Break Time Remaining 10:00.
Turing Machines.
Table 12.1: Cash Flows to a Cash and Carry Trading Strategy.
PP Test Review Sections 6-1 to 6-6
User Friendly Price Book Maintenance A Family of Enhancements For iSeries 400 DMAS from Copyright I/O International, 2006, 2007, 2008, 2010 Skip Intro.
Bright Futures Guidelines Priorities and Screening Tables
EIS Bridge Tool and Staging Tables September 1, 2009 Instructor: Way Poteat Slide: 1.
Bellwork Do the following problem on a ½ sheet of paper and turn in.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 24.1 Test-Driving the Ticket Information Application.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Tutorial 12 – Security Panel Application Introducing.
Exarte Bezoek aan de Mediacampus Bachelor in de grafische en digitale media April 2014.
Copyright © 2013, 2009, 2006 Pearson Education, Inc. 1 Section 5.5 Dividing Polynomials Copyright © 2013, 2009, 2006 Pearson Education, Inc. 1.
Copyright © 2012, Elsevier Inc. All rights Reserved. 1 Chapter 7 Modeling Structure with Blocks.
 Copyright I/O International, 2013 Visit us at: A Feature Within from Item Class User Friendly Maintenance  Copyright.
1 RA III - Regional Training Seminar on CLIMAT&CLIMAT TEMP Reporting Buenos Aires, Argentina, 25 – 27 October 2006 Status of observing programmes in RA.
Basel-ICU-Journal Challenge18/20/ Basel-ICU-Journal Challenge8/20/2014.
1..
Lilian Blot PART III: ITERATIONS Core Elements Autumn 2012 TPOP 1.
CONTROL VISION Set-up. Step 1 Step 2 Step 3 Step 5 Step 4.
Adding Up In Chunks.
1 10 pt 15 pt 20 pt 25 pt 5 pt 10 pt 15 pt 20 pt 25 pt 5 pt 10 pt 15 pt 20 pt 25 pt 5 pt 10 pt 15 pt 20 pt 25 pt 5 pt 10 pt 15 pt 20 pt 25 pt 5 pt Synthetic.
: 3 00.
5 minutes.
1 hi at no doifpi me be go we of at be do go hi if me no of pi we Inorder Traversal Inorder traversal. n Visit the left subtree. n Visit the node. n Visit.
Types of selection structures
Speak Up for Safety Dr. Susan Strauss Harassment & Bullying Consultant November 9, 2012.
Chapter 12 Working with Forms Principles of Web Design, 4 th Edition.
Essential Cell Biology
Exponents and Radicals
Clock will move after 1 minute
PSSA Preparation.
Essential Cell Biology
Immunobiology: The Immune System in Health & Disease Sixth Edition
Physics for Scientists & Engineers, 3rd Edition
Energy Generation in Mitochondria and Chlorplasts
Select a time to count down from the clock above
Murach’s OS/390 and z/OS JCLChapter 16, Slide 1 © 2002, Mike Murach & Associates, Inc.
Copyright Tim Morris/St Stephen's School
1 Decidability continued…. 2 Theorem: For a recursively enumerable language it is undecidable to determine whether is finite Proof: We will reduce the.
Slide 6a-1 CHAPTER 6 Matching Patterns: Using Regular expressions to match patterns.
Copyright © 2003 Pearson Education, Inc. Slide 6a-1 The Web Wizard’s Guide to PHP by David Lash.
Copyright © 2003 Pearson Education, Inc. Slide 6a-1 The Web Wizard’s Guide to PHP by David Lash.
Introduction to Programming the WWW I CMSC Winter 2004 Lecture 13.
Presentation transcript:

Copyright © 2003 Pearson Education, Inc. Slide 6a-1 The Web Wizards Guide to PHP by David Lash

Copyright © 2003 Pearson Education, Inc. Slide 6a-2 CHAPTER 6 Matching Patterns: Using Regular expressions to match patterns

Copyright © 2003 Pearson Education, Inc. Objectives To learn to use PHP pattern-matching functions and regular expressions to match character string patterns and filter input data

Copyright © 2003 Pearson Education, Inc. Slide 6a-4 Why Match Patterns Scripting problem may require: verification of input from form was input a 7 digit phone number parsing input from a file FirstName:LastName:Age:Salary PHP supports three pattern matching functions: ereg(), split(), and ereg_replace(). Regular expressions are used to define very specific match patterns

Copyright © 2003 Pearson Education, Inc. Slide 6a-5 The ereg() function Use ereg() to check if a string contains a match pattern:

Copyright © 2003 Pearson Education, Inc. Slide 6a-6 For example Consider the following $name = 'Jake Jackson'; $pattern = 'ke'; if (ereg($pattern, $name)){ print 'Match'; } else { print 'No match'; } This code outputs Match since the string ke is found. if $pattern was aa the above code segment would output No match

Copyright © 2003 Pearson Education, Inc. Slide 6a-7 What are regular expressions? Special pattern matching characters with specific pattern matching meanings. Their meanings are defined by an industry standard (the IEEE POSIX standard). For example,a caret symbol (^) returns a match when the pattern that follows starts the target string. $part = 'AA100'; $pattern = '^AA'; if (ereg($pattern, $part)) { print 'Match'; } else { print 'No match'; } Check if $part starts with AA Would be output if $part was AB100, 100AA, or Apple.

Copyright © 2003 Pearson Education, Inc. Slide 6a-8 Selected Pattern Matching Characters

Copyright © 2003 Pearson Education, Inc. Slide 6a-9 Selected Pattern Matching Characters

Copyright © 2003 Pearson Education, Inc. Slide 6a-10 Selected Pattern Matching Characters

Copyright © 2003 Pearson Education, Inc. Slide 6a-11 For example... The following uses ereg() and eregi() (case insensitive compare) Enter product code (Use AA## format): Please enter description: Asks for a product code and description (not to contain Boat or Plane.

Copyright © 2003 Pearson Education, Inc. Slide 6a-12 A Full Script Example Consider an example script that enables end- user to select multiple items from a checklist. A survey about menu preferences Wil look at how to send multiple items and how to receive them (later)

Copyright © 2003 Pearson Education, Inc. Slide 6a-13 A Full Example Product Information Results 2. 3.<?php 4. $products = array('AB01'=>'25-Pound Sledgehammer', 'AB02'=>'Extra Strong Nails', 'AB03'=>'Super Adjustable Wrench', 'AB04'=>'3-Speed Electric Screwdriver'); 5. if (eregi('boat|plane', $description)){ 6. print 'Sorry, we do not sell boats or planes anymore'; 7. } elseif (ereg('^AB', $code)){ 8. if (isset($products["$code"])){ 9. print "Code $code Description: $products[$code]"; 10. } else { 11. print 'Sorry, product code not found'; 12. } 13. } else { 14. print 'Sorry, all our product codes start with "AB"'; 15.} ?> Create a list of products. Check if boat or plane. Check if valid product number

Copyright © 2003 Pearson Education, Inc. Slide 6a-14 A Full Example with REGISTER_GLOBALS Off Product Information Results 2. 3.<?php $code = $_POST[code]; 4. $products = array('AB01'=>'25-Pound Sledgehammer', 'AB02'=>'Extra Strong Nails', 'AB03'=>'Super Adjustable Wrench', 'AB04'=>'3-Speed Electric Screwdriver'); 5. if (eregi('boat|plane', $description)){ 6. print 'Sorry, we do not sell boats or planes anymore'; 7. } elseif (ereg('^AB', $code)){ 8. if (isset($products["$code"])){ 9. print "Code $code Description: $products[$code]"; 10. } else { 11. print 'Sorry, product code not found'; 12. } 13. } else { 14. print 'Sorry, all our product codes start with "AB"'; 15.} ?> Create a list of products. Check if boat or plane. Check if valid product number. Receive $code

Copyright © 2003 Pearson Education, Inc. Slide 6a-15 The Output... The previous code can be executed at

Copyright © 2003 Pearson Education, Inc. Slide 6a-16 Using grouping characters Use parentheses to specify a group of characters in a regular expression. Above uses parentheses with | to indicate Dav can be followed by e or id.

Copyright © 2003 Pearson Education, Inc. Slide 6a-17 Using grouping characters Now add in ^ and $ characters...

Copyright © 2003 Pearson Education, Inc. Slide 6a-18 Use curly brackets to specify a range of characters Use curly brackets to look for a repeating of one or more characters E.g., L{3} matches 3 Ls L{3,} matches3 or more Ls L{2,4} matchs 2 to 4 Ls

Copyright © 2003 Pearson Education, Inc. Slide 6a-19 Use square brackets for character classes Use square brackets to match one of character found inside them

Copyright © 2003 Pearson Education, Inc. Slide 6a-20 Use square brackets with range More common to specify a range of matches For exampe [0-9], [a-z] or [A-Z] Or use multiple characters at once...

Copyright © 2003 Pearson Education, Inc. Slide 6a-21 Using caret ^ and square brackets When caret ^ is first character within square brackets it means not. Note: Within a character class, as in [^... ], ^ means not. Earlier saw how it can indicate that the character that follows the caret symbol starts the match pattern

Copyright © 2003 Pearson Education, Inc. Slide 6a-22 Special Pre-defined character classes

Copyright © 2003 Pearson Education, Inc. Slide 6a-23 Special Pre-defined character classes

Copyright © 2003 Pearson Education, Inc. Slide 6a-24 Special Pre-defined character classes

Copyright © 2003 Pearson Education, Inc. Slide 6a-25 Building Regular Expressions that work Building Regular expressions is best done incrementally Lets look at a process to build a regular expression to validate a date input field: mm/dd/yyyy format (for example, 01/05/2002 but not 1/5/02 ).

Copyright © 2003 Pearson Education, Inc. Slide 6a-26 Building Regular Expressions that work 1. Determine the precise field rules. what is valid input and invalid input. You might decide to allow 09/09/2002 but not 9/9/2002 or Sep/9/2002 as valid date formats. Work through several examples as follows:

Copyright © 2003 Pearson Education, Inc. Slide 6a-27 Building an Example Regular Expression 1. Determine the precise field rules. What is valid input and invalid input? You might decide to allow 09/09/2002 but not 9/9/2002 or Sep/9/2002 Work through several examples as follows:

Copyright © 2003 Pearson Education, Inc. Slide 6a-28 Building an Example Regular Expression 2. Get the form and form-handling scripts working Build the input form and a bare bonesreceiving script For example: receives input of 1 or more characters: if ( ereg(.+, $date ) ) { print "Valid date= $date"; } else { print "Invalid date= $date"; }

Copyright © 2003 Pearson Education, Inc. Slide 6a-29 Building an Example Regular Expression 3. Start with the most specific term possible You know must have 2 slashes between 2 character month, 2 character day and 4 character year So change receiving script to: if ( ereg(../../...., $date ) ) { print "Valid date= $date"; } else { print "Invalid date= $date"; } So 12/21/1234 and fj/12/ffff are valid, but 1/1/11 is not.

Copyright © 2003 Pearson Education, Inc. Slide 6a-30 Building an Example Regular Expression 4. Anchor the parts you can. Add the ^ and $ quantifiers where possible. Also, can add the [[:digit:]] character class to require numbers instead of any character. So change receiving script to: $two=[[:digit:]]{2}; if ( ereg("^$two/$two/$two$two$", $date ) ) { print "Valid date= $date"; } else { print "Invalid date= $date"; } So 01/16/2003, 09/09/2005, 01/12/1211, and 99/99/9999 are valid dates.

Copyright © 2003 Pearson Education, Inc. Slide 6a-31 Building an Example Regular Expression 5. Get more specific if possible... You might note that three more rules can be added: The first digit of the month can be only 0, or 1. For example, 25/12/2002 is clearly illegal. The first digit of a day can be only 0, 1, 2, or 3. For example, 05/55/2002 is clearly illegal. Only allow years from this century allowed. Dont care about dates like 05/05/1928 or 05/05/3003. $two=[[:digit:]]{2}; $month=[0-1][[:digit:]]; $day=[0-3][[:digit:]]; $year="2[[:digit:]]$two"; if ( ereg("^($month)/($day)/($year)$", $date ) ) { Now input like 09/99/2001 and 05/05/4000 is illegal.

Copyright © 2003 Pearson Education, Inc. Slide 6a-32 A Full Script Example Consider an example script that asks end-user for a date Use regular expressions to validate Use the following HTML input

Copyright © 2003 Pearson Education, Inc. Slide 6a-33 A Full Example Decsions <?php 5. $two=[[:digit:]]{2}; 6. $month=[0-3][[:digit:]]; 7. $day=[0-3][[:digit:]]; 8. $year="2[[:digit:]]$two"; 9. if ( ereg("^($month)/($day)/($year)$", $date ) ) { 10. print "Got valid date=$date "; 11. } else { 12. print "Invalid date=$date"; 13. } 14. ?> Use same regular expression as before

Copyright © 2003 Pearson Education, Inc. Slide 6a-34 A Full Example with REGISTER_GLOBALS Off Decsions <?php $date = $_POST[date]; 5. $two=[[:digit:]]{2}; 6. $month=[0-3][[:digit:]]; 7. $day=[0-3][[:digit:]]; 8. $year="2[[:digit:]]$two"; 9. if ( ereg("^($month)/($day)/($year)$", $date ) ) { 10. print "Got valid date=$date "; 11. } else { 12. print "Invalid date=$date"; 13. } 14. ?> Use same regular expression as before

Copyright © 2003 Pearson Education, Inc. Slide 6a-35 The Output... The previous code can be executed at

Copyright © 2003 Pearson Education, Inc. Slide 6a-36 Matching Patterns With split() Use split() to break a string into different pieces based on the presence of a match pattern.

Copyright © 2003 Pearson Education, Inc. Slide 6a-37 Matching Patterns With split() Consider another example; $line = Baseball, hot dogs, apple pie; $item = split(,, $line ); print ("0=$item[0] 1=$item[1] 2=$item[2]"); These lines will have the following output: 0=Baseball 1= hot dogs 2= apple pie

Copyright © 2003 Pearson Education, Inc. Slide 6a-38 Matching Patterns With split() When you know how many patterns you are interested can use list() along with split(): line = AA1234:Hammer:122:12; list($partno, $part, $num, $cost) = split(:, $line, 4); print "partno=$partno part=$part num=$num cost=$cost"; The above code would output the following: partno=AA1234 part=Hammer num=122 cost=12

Copyright © 2003 Pearson Education, Inc. Slide 6a-39 Example of split() As an example of split() consider the following: $line = Please, pass thepepper; $result = split( [[:space:]]+, $line ); Will results in the following: $result[0] = Please; $result[1] =, $result[2] = pass; $result[3] = thepepper;

Copyright © 2003 Pearson Education, Inc. Slide 6a-40 A Full Script Example Consider an example script that updates the date checker just studied: Uses split() to further refine date validation Uses the same input form:

Copyright © 2003 Pearson Education, Inc. Slide 6a-41 A Full Example Date Check <?php 5. $two=[[:digit:]]{2}; 6. $month=[0-3][[:digit:]]; 7. $day=[0-3][[:digit:]]; 8. $year="2[[:digit:]]$two"; 9. if ( ereg("^($month)/($day)/($year)$", $date ) ) { 10. list($mon, $day, $year) = split( /, $date ); 11. if ( $mon >= 1 && $mon <= 12 ) { 12. if ( $day <= 31 ) { 13. print "Valid date mon=$mon day=$day year=$year"; 14. } else { 15. print " Illegal day specifed Day=$day"; 16. } 17. } else { 18. print " Illegal month specifed Mon=$mon"; 19. } 20. } else { 21. print ("Invalid date format= $date"); 22. } 23. ?> Use split() and list() to get month, day and year.

Copyright © 2003 Pearson Education, Inc. Slide 6a-42 A Full Example with REGISTER_GLOBALS Off Date Check <?php $date = $_POST[date]; 5. $two=[[:digit:]]{2}; 6. $month=[0-3][[:digit:]]; 7. $day=[0-3][[:digit:]]; 8. $year="2[[:digit:]]$two"; 9. if ( ereg("^($month)/($day)/($year)$", $date ) ) { 10. list($mon, $day, $year) = split( /, $date ); 11. if ( $mon >= 1 && $mon <= 12 ) { 12. if ( $day <= 31 ) { 13. print "Valid date mon=$mon day=$day year=$year"; 14. } else { 15. print " Illegal day specifed Day=$day"; 16. } 17. } else { 18. print " Illegal month specifed Mon=$mon"; 19. } 20. } else { 21. print ("Invalid date format= $date"); 22. } 23. ?> Use split() and list() to get month, day and year.

Copyright © 2003 Pearson Education, Inc. Slide 6a-43 The Output... The previous code can be executed at

Copyright © 2003 Pearson Education, Inc. Slide 6a-44 Using eregreplace() Use ereg_replace() when replacing characters in a string variable. It can be used to replace one string pattern for another in a string variable. For example: $start = AC1001:Hammer:15:150; $end = ereg_replace( Hammer, Drill, $start ); print "end=$end"; The above script segment would output : end=AC1001:Drill:15:150

Copyright © 2003 Pearson Education, Inc. Slide 6a-45 Summary PHP supports a set of operators and functions that are useful for matching and manipulating patterns in strings: The ereg() function looks for and match patterns The split() function uses a pattern to split string values into as many pieces as there are matches. Regular expressions greatly enhance its pattern matching capabilities.

Copyright © 2003 Pearson Education, Inc. Slide 6a-46 Summary Associative arrays use string value indices rather than numerical values. They are useful for cross-referencing an index with a value. You can use the foreach loop to concisely examine the various items within an associative array.

Copyright © 2003 Pearson Education, Inc. Slide 6a-47 Summary Use conditional statements to test for certain conditions and, based on the results of the test, to run specific script statements. Loops expand the types of programming problems that you can solve and allow you to solve some programming problems much more concisely Use logical AND (&&), OR (||) and NOT (!) operators to carry out compound tests.

Copyright © 2003 Pearson Education, Inc. Slide 6a-48 Summary Variables are used to store and access data in computer memory. You can associate a value with a variable, change that value, print it out, and perform many different operations on it. PHP supports both numeric and string variables. String variables use different methods for value manipulation (for example, concatenation) than numeric variables do.