Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 19 PHP Part II Credits: Parts of the slides are based on slides created by textbook authors, P.J. Deitel and H. M. Deitel by Prentice Hall ©1992-2012.

Similar presentations


Presentation on theme: "Chapter 19 PHP Part II Credits: Parts of the slides are based on slides created by textbook authors, P.J. Deitel and H. M. Deitel by Prentice Hall ©1992-2012."— Presentation transcript:

1 Chapter 19 PHP Part II Credits: Parts of the slides are based on slides created by textbook authors, P.J. Deitel and H. M. Deitel by Prentice Hall © by Pearson Education, Inc. All Rights Reserved.

2 19.6 String Comparison A regular expression is a series of characters used for pattern-matching templates in strings, text files and databases. Many string-processing tasks can be accomplished using the equality and relational operators (==, !=, <, <=, > and >=). Function strcmp compares two strings. The function returns -1 if the first string alphabetically precedes the second string, 0 if the strings are equal, and 1 if the first string alphabetically follows the second.

3 Outline compare.php (1 of 2)
Checks whether the ith element of the fruits array preceeds the string banana

4 compare.php (2 of 2) Uses relational operators to compare the element of the fruits array with the string apple

5 19.7 String Processing and Regular Expressions
Goal: find pattern in text Regular expression (as ereg ) – specially formated strings used to find patterns in text Functions ereg and preg_match use regular expressions to search a string for a specified pattern. ereg( "Now", $search ) If a pattern is found using ereg, it returns the length of the matched first string-which evaluates to true in a boolean context. Function ereg receives a regular expression pattern to search for and the string to search. The optional third argument to function ereg is an array that stores matches to each parenthetical statement of the regular expression. The first element stores the string matched for the entire pattern, and the remaining elements are indexed from left to right. Function eregi performs case-insensitive pattern matches. To find multiple instances of a given pattern, we must make multiple calls to ereg, and remove matched instances before calling the function again by using a function such as ereg_replace.

6 19.7 String Processing and Regular Expressions
Anything enclosed in single quotes in a print statement is not interpolated (unless the single quotes are nested in a double-quoted string literal). Regular expressions can include metacharacters that specify patterns. the caret (^) metacharacter matches the beginning of a string, while the dollar sign ($) matches the end of a string. the period (.) metacharacter matches any single character. Bracket expressions are lists of characters enclosed in square brackets ([]) that match any single character from the list. Ranges can be specified by supplying the beginning and the end of the range separated by a dash (-). ex: [a-zA-Z] The special bracket expressions [[:<:]] and [[:>:]] match the beginning and end of a word, respectively. Or it can be used \b and \b in ed5 Quantifiers are used in regular expressions to denote how often a particular character or set of characters can appear in a match. Regular expression also referred to as regex or regexp, provides a concise and flexible means for matching strings of text, such as particular characters, words, or patterns of characters.

7 PHP character classes is a group of characters that might
Fig | Some PHP quantifiers. PHP character classes is a group of characters that might appear in a string. Fig | Some PHP character classes.

8 19.7 String Processing and Regular Expressions
A character classes are enclosed by the delimiter . [: and :]. When this expression is placed in another set of brackets, such as [[:alpha:]] in line 38, it’s matching a single ch-r that’s a member of the class. A bracketed expression containing two or more adjacent character classes in the class delimiters represents those character sets combined [[:upper:][:lower]]* - represents all strings of uppercase and lowercase in any order [[:upper:]][[:lower]]* - matches all strings that alternate between uppercase and lowercase chrs (starting with uppercase and ending with lowercase). Function ereg_replace takes 3 arguments— the pattern to match, a string to replace the matched string and the string to search. $search = ereg_replace( $match[ 1 ], "", $search ); The modified string is returned. Regular expression also referred to as regex or regexp, provides a concise and flexible means for matching strings of text, such as particular characters, words, or patterns of characters.

9 Outline expression.php String to search
Searches for the string “Now” in $search Checks if string “Now” appears at the beginning of $search Checks if string “Now” appears at the end of $search expression.php (1 of 2)

10 Outline expression.php
Searches for a word ending in “ow” and stores matches in $match array Quantifier * matches the preceding pattern 0 or more times expression.php (2 of 2) $match[0] stores the string matches for the entire pattern Prints first $match[1] encountered instance of word ending in “ow”; Second encountered instance of word ending in “ow” will be in $match[2] Function ereg_replace takes 3 arguments the pattern to match, a string to replace the matched string and the string to search. The modified string is returned. Performs a in the loop case-insensitive search for words beginning with the letter “t” and then [[:alpha:]]+ i.e. [a-zA-Z]) = a single ch-r Replaces the found instance from the previous call to eregi with an empty string so that the next instance of the pattern can be found and stored in $match To find multiple instances of a given pattern, we must make multiple calls to eregi, and remove matched instances before calling the function again by using a function such as ereg_replace.

11 ©1992-2012 by Pearson Education, Inc. All Rights Reserved.

12 To find multiple instances of a given pattern, we must make multiple calls to preg_match, and remove matched instances before calling the function again by using a function such as preg_replace.

13 19.8 Form Processing and Business Logic
Superglobal arrays are associative arrays predefined by PHP that hold variables acquired from user input, the environment or the web server and are accessible in any variable scope. The arrays $_GET and $_POST retrieve information sent to the server by HTTP get and post requests, respectively. Using method = "post" appends form data to the browser request that contains the protocol and the requested resource’s URL. Scripts located on the web server’s machine can access the form data sent as part of the request.

14 $_REQUEST Contains all the variables that
appears in both $_GET and $_POST Fig | Some useful superglobal arrays.

15 Outline form.html (1 of 4) Appends form data to the browser request that contains the protocol and the URL of the requested resource Form data is posted to form.php to be processed

16 Outline form.html (2 of 4) Creates form fields
Creates drop-down list with book names

17 Outline form.html (3 of 4) Creates radio buttons with “Windows XP” initially selected

18 Outline form.html (4 of 4)

19 Good Programming Practice 19.1
Use meaningful (and self-explanatory) HTML/XHTML object names for input fields. This makes PHP scripts that retrieve form data easier to understand.

20 19.8 Form Processing and Business Logic
Function extract creates a variable/value pair corresponding to each key/value pair in the associative array passed as an argument. Business logic, or business rules, ensures that only valid information is stored in databases. We escape the normal meaning of a character in a string by preceding it with the backslash character (\). Function die terminates script execution. The function’s optional argument is a string or an integer, which is printed as the script exits. - If it is an integer, it’s used as a return status code – typically in command-line PHP shell script)

21 form.php (1 of 5)

22 Creates a variable/value pair for each key/value pair in $_POST
Ensures that phone number is in proper format Terminates execution and closes the document properly

23 Outline form.php (3 of 5) Prints the value entered in the field in form.html

24

25

26 Software Engineering Observation 19.1
Use business logic to ensure that invalid information is not stored in databases. When possible, validate important or sensitive form data on the server, since JavaScript may be disabled by the client. Some data, such as passwords, must always be validated on the server side.

27 Error-Prevention Tip 19.3 Be sure to close any open HTML/XHTML tags when calling function die. Not doing so can produce invalid HTML/XHTML output that will not display properly in the client browser. Function die has an optional parameter that specifies a message to output when exiting, so one technique for closing tags is to close all open tags using die, as in die("</body></html>").

28


Download ppt "Chapter 19 PHP Part II Credits: Parts of the slides are based on slides created by textbook authors, P.J. Deitel and H. M. Deitel by Prentice Hall ©1992-2012."

Similar presentations


Ads by Google