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.

Slides:



Advertisements
Similar presentations
JavaScript I. JavaScript is an object oriented programming language used to add interactivity to web pages. Different from Java, even though bears some.
Advertisements

String and Lists Dr. Benito Mendoza. 2 Outline What is a string String operations Traversing strings String slices What is a list Traversing a list List.
 2005 Pearson Education, Inc. All rights reserved Introduction.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 14 Web Database Programming Using PHP.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Introduction to Scripting.
Dr. Qusai Abuein1 Internet & WWW How to program Chap.(26) PHP (Personal Home Page)
 2008 Pearson Education, Inc. All rights reserved JavaScript: Control Statements II.
Introduction to scripting
 2004 Prentice Hall, Inc. All rights reserved. Chapter 25 – Perl and CGI (Common Gateway Interface) Outline 25.1 Introduction 25.2 Perl 25.3 String Processing.
Form Handling, Validation and Functions. Form Handling Forms are a graphical user interfaces (GUIs) that enables the interaction between users and servers.
Regular Expressions in ColdFusion Applications Dave Fauth DOMAIN technologies Knowledge Engineering : Systems Integration : Web.
Last Updated March 2006 Slide 1 Regular Expressions.
Advance web Programming Chapter 3: MySQL Date: 28 April 2014 Advance web Programming Chapter 3: MySQL Date: 28 April 2014 Dr. Mogeeb A. A. Mosleh .
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 26 - PHP Outline 26.1 Introduction 26.2 PHP 26.3 String Processing and Regular Expressions 26.4.
Web forms in PHP Forms Recap  Way of allowing user interaction  Allows users to input data that can then be processed by a program / stored in a back-end.
1 Chapter 6 – Creating Web Forms and Validating User Input spring into PHP 5 by Steven Holzner Slides were developed by Jack Davis College of Information.
Chapter 4 – The Building Blocks Data Types Literals Variables Constants.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 26 - PHP Outline 26.1 Introduction 26.2 PHP 26.3 String Processing and Regular Expressions 26.4.
 2008 Pearson Education, Inc. All rights reserved PHP.
Jozef Goetz contribution, Credits: Parts of the slides are based on slides created by textbook authors, P.J. Deitel and H. M. Deitel by Prentice.
 2003 Prentice Hall, Inc. All rights reserved. CHAPTER 3 JavaScript 1.
COP 3813 Intro to Internet Computing Prof. Roy Levow PHP.
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
Chap 3 – PHP Quick Start COMP RL Professor Mattos.
ASP.NET Programming with C# and SQL Server First Edition Chapter 5 Manipulating Strings with C#
 2003 Prentice Hall, Inc. All rights reserved. CHAPTER 3 JavaScript 1.
Linux+ Guide to Linux Certification, Third Edition
 Pearson Education, Inc. All rights reserved Introduction to Java Applications.
Regular Expressions in PHP. Supported RE’s The most important set of regex functions start with preg. These functions are a PHP wrapper around the PCRE.
When you read a sentence, your mind breaks it into tokens—individual words and punctuation marks that convey meaning. Compilers also perform tokenization.
 2008 Pearson Education, Inc. All rights reserved PHP.
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Introduction to Scripting.
8 Chapter Eight Server-side Scripts. 8 Chapter Objectives Create dynamic Web pages that retrieve and display database data using Active Server Pages Process.
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
Part:2.  Keywords are words with special meaning in JavaScript  Keyword var ◦ Used to declare the names of variables ◦ A variable is a location in the.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Introduction to Scripting.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 14 Web Database Programming Using PHP.
REEM ALMOTIRI Information Technology Department Majmaah University.
String and Lists Dr. José M. Reyes Álamo. 2 Outline What is a string String operations Traversing strings String slices What is a list Traversing a list.
Web Database Programming Using PHP
Receiving form Variables
Chapter 6 JavaScript: Introduction to Scripting
Chapter 26 - PHP Outline 26.1 Introduction 26.2 PHP
© 2016 Pearson Education, Ltd. All rights reserved.
CIIT-Human Computer Interaction-CSC456-Fall-2015-Mr
Web Database Programming Using PHP
PHP Functions Besides the built-in PHP functions, we can create our own functions. A function is a block of statements that can be used repeatedly in.
19.10 Using Cookies A cookie is a piece of information that’s stored by a server in a text file on a client’s computer to maintain information about.
Chapter 19 PHP Part III Credits: Parts of the slides are based on slides created by textbook authors, P.J. Deitel and H. M. Deitel by Prentice Hall ©
23 PHP.
Introduction to Scripting
Intro to PHP & Variables
Working with Forms and Regular Expressions
HTML Forms and User Input
More Selections BIS1523 – Lecture 9.
Chapter 8 JavaScript: Control Statements, Part 2
WEB PROGRAMMING JavaScript.
Arrays We often want to organize objects or primitive data in a way that makes them easy to access and change. An array is simple but powerful way to.
Introduction to Classes and Objects
PHP.
Web DB Programming: PHP
String and Lists Dr. José M. Reyes Álamo.
Data Manipulation & Regex
PHP PART 2.
JavaScript: Introduction to Scripting
PHP an introduction.
23 PHP.
PHP-II.
Presentation transcript:

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 by Pearson Education, Inc. All Rights Reserved.

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.

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

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

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.

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.

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

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.

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)

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.

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

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.

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.

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

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

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

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

Outline form.html (4 of 4)

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.

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)

form.php (1 of 5)

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

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

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.

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>").