PHP Programming with MySQL Slide 5-1 CHAPTER 5 Manipulating Strings.

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

C Characters & Strings Character Review Character Handling Library Initialization String Conversion Functions String Handling Library Standard Input/Output.
Lecture 9. Lecture 9: Outline Strings [Kochan, chap. 10] –Character Arrays/ Character Strings –Initializing Character Strings. The null string. –Escape.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Characters and Strings.
Current Assignments Homework 5 will be available tomorrow and is due on Sunday. Arrays and Pointers Project 2 due tonight by midnight. Exam 2 on Monday.
Chapter 10.
Chapter 3 Manipulating Strings PHP Programming with MySQL 2nd Edition
Chapter 8 Characters and Strings Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
CSCI 116 Decisions & Validation. Overview Constants Boolean conditions Decision logic Form validation 2.
Manipulating Strings. 2 Topics Manipulate strings Manipulate strings Parse strings Parse strings Compare strings Compare strings Handle form submissions.
Chapter 9: Arrays and Strings
Tutorial 14 Working with Forms and Regular Expressions.
JavaScript, Fourth Edition
JavaScript, Third Edition
Chapter 7. 2 Objectives You should be able to describe: The string Class Character Manipulation Methods Exception Handling Input Data Validation Namespaces.
IS 1181 IS 118 Introduction to Development Tools Chapter 4 String Manipulation and Regular Expressions.
Operations & Strings CSCI 116 Web Programming I. 2 Arithmetic Operators Arithmetic operators are used to perform mathematical calculations.
Manipulating Strings. Name of Book2 Objectives Manipulate strings Parse strings Compare strings Handle form submissions.
 2007 Pearson Education, Inc. All rights reserved C Characters and Strings.
XP Tutorial 14 New Perspectives on HTML, XHTML, and DHTML, Comprehensive 1 Working with Forms and Regular Expressions Validating a Web Form with JavaScript.
Chapter 4 Handling User Input PHP Programming with MySQL 2nd Edition
Strings and Arrays. String Is a sequence of characters. Example: “hello”, “how are you?”, “123”,and are all valid string values.
Chapter 4 – The Building Blocks Data Types Literals Variables Constants.
Tutorial 14 Working with Forms and Regular Expressions.
Chapter 3 Manipulating Strings PHP Programming with MySQL 2nd Edition
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 9 More About Strings.
Chap 3 – PHP Quick Start COMP RL Professor Mattos.
2440: 211 Interactive Web Programming Expressions & Operators.
Chapter 3: Data Types and Operators JavaScript - Introductory.
Chapter 10 Selected Single-Row Functions Oracle 10g: SQL.
Mr. Justin “JET” Turner CSCI 3000 – Fall 2015 CRN Section A – TR 9:30-10:45 CRN – Section B – TR 5:30-6:45.
Chapter 8 Cookies And Security JavaScript, Third Edition.
ASP.NET Programming with C# and SQL Server First Edition Chapter 5 Manipulating Strings with C#
Chapter 1 Getting Started with PHP PHP Programming with MySQL
Strings in PHP Working with Text in PHP Strings and String Functions Mario Peshev Technical Trainer Software University
PHP Programming with MySQL Slide 4-1 CHAPTER 4 Functions and Control Structures.
Oracle 11g: SQL Chapter 10 Selected Single-Row Functions.
1Computer Sciences Department Princess Nourah bint Abdulrahman University.
What is PHP? PHP stands for PHP: Hypertext Preprocessor PHP is a server-side scripting language, like ASP PHP scripts are executed on the server PHP supports.
Working with Forms and Regular Expressions Validating a Web Form with JavaScript.
Strings, output, quotes and comments
Representing Strings and String I/O. Introduction A string is a sequence of characters and is treated as a single data item. A string constant, also termed.
4. Javascript M. Udin Harun Al Rasyid, S.Kom, Ph.D Lab Jaringan Komputer (C-307) Desain.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Introduction to Scripting.
Basic Variables & Operators Web Programming1. Review: Perl Basics Syntax ► Comments: start with # (ignored by Perl) ► Statements: ends with ; (performed.
C++ for Engineers and Scientists Second Edition Chapter 7 Completing the Basics.
PHP Programming with MySQL Slide 3-1 CHAPTER 3 Working with Data Types and Operators.
 2003 Prentice Hall, Inc. All rights reserved. 11 Fundamentals of Characters and Strings Character constant –Integer value of a character –Single quotes.
UniMAP SEM I - 10/11EKT 120 Computer Programming1 Lecture 8 – Arrays (2) and Strings.
Chapter 14 JavaScript: Part II The Web Warrior Guide to Web Design Technologies.
Chapter 1 Getting Started with PHP 2 nd Edition. 2 Objectives In this chapter you will: Create PHP scripts Create PHP code blocks Work with variables.
XP Tutorial 7 New Perspectives on JavaScript, Comprehensive 1 Working with Forms and Regular Expressions Validating a Web Form with JavaScript.
Basic Scripting & Variables Yasar Hussain Malik - NISTE.
A FIRST BOOK OF C++ CHAPTER 14 THE STRING CLASS AND EXCEPTION HANDLING.
1 Arrays and Pointers The name of an array is a pointer constant to the first element. Because the array’s name is a pointer constant, its value cannot.
Principles of Programming - NI Chapter 10: Character & String : In this chapter, you’ll learn about; Fundamentals of Strings and Characters The difference.
1 Agenda  Unit 7: Introduction to Programming Using JavaScript T. Jumana Abu Shmais – AOU - Riyadh.
Lesson 4 String Manipulation. Lesson 4 In many applications you will need to do some kind of manipulation or parsing of strings, whether you are Attempting.
Strings CSCI 112: Programming in C.
Chapter 10 Selected Single-Row Functions Oracle 10g: SQL
C Characters and Strings
Arrays: Checkboxes and Textareas
JavaScript Objects.
Introduction to Scripting
Chapter 3 Manipulating Strings PHP Programming with MySQL 2nd Edition Modified by Anita Philipp –Spring 2011.
Chapter 8 JavaScript: Control Statements, Part 2
PHP.
String functions
Web Programming Language
String functions
Presentation transcript:

PHP Programming with MySQL Slide 5-1 CHAPTER 5 Manipulating Strings

PHP Programming with MySQL Slide 5-2 Objectives Manipulate strings Parse strings Compare strings Handle form submissions

PHP Programming with MySQL Slide 5-3 Constructing Text Strings A text string contains zero or more characters surrounded by double or single quotation marks Text strings can be used as literal values or assigned to a variable echo " Dr. Livingstone, I presume? "; $Explorer = "Henry M. Stanley"; echo $Explorer; Text strings can also be surrounded with single quotation marks

PHP Programming with MySQL Slide 5-4 Constructing Text Strings To include a quoted string within a literal string surrounded by double quotation marks, you surround the quoted string with single quotation marks To include a quoted string within a literal string surrounded by single quotation marks, you surround the quoted string with double quotation marks

PHP Programming with MySQL Slide 5-5 Constructing Text Strings $ExplorerQuote = ' "Dr. Livingstone, I presume?" '; echo $ExplorerQuote; Figure 5-1 Output of a text string containing double quotation marks

PHP Programming with MySQL Slide 5-6 mail() Function The syntax for the mail() function is: mail(recipient(s), subject, message[,additional_headers]) The mail() function returns a value of true if a message was delivered successfully or false if it was not $To = $Subject = "This is the subject"; $Message = "This is the message."; $Headers="From: Don Gosselin "; mail($To, $Subject, $Message, $Headers);

PHP Programming with MySQL Slide 5-7 mail() Function Figure 5-2 PHP .html in a Web browser

PHP Programming with MySQL Slide 5-8 Working with String Operators In PHP, you use two operators to combine strings Concatenation operator (.) $Destination = "Paris"; $Location = "France"; $Destination = " “. $Destination. “ is in “. $Location. ". "; echo $Destination;

PHP Programming with MySQL Slide 5-9 Working with String Operators Concatenation assignment operator (.=) $Destination = " Paris"; $Destination.= "is in France. "; echo $Destination;

PHP Programming with MySQL Slide 5-10 Adding Escape Characters and Sequences An escape character tells the compiler or interpreter that the character that follows it has a special purpose In PHP, the escape character is the backslash \ echo ' Marilyn Monroe\'s real name was Norma Jean Baker. '; Do not add a backslash before an apostrophe if you surround the text string with double quotation marks echo " Marilyn Monroe's real name was Norma Jean Baker. ";

PHP Programming with MySQL Slide 5-11 Adding Escape Characters and Sequences The escape character combined with one or more other characters is called an escape sequence Table 5-1 PHP escape sequences within double quotation marks

PHP Programming with MySQL Slide 5-12 Adding Escape Characters and Sequences $Explorer = "Henry M. Stanley"; echo " \"Dr. Livingstone, I presume?\“ asked $Explorer. "; Figure 5-3 Output of literal text containing double quotation escape sequences

PHP Programming with MySQL Slide 5-13 Simple and Complex String Syntax Simple string syntax uses the value of a variable within a string by including the variable name inside a text string with double quotation marks $Vegetable = "broccoli"; echo " Do you have any $Vegetable? "; When variables are placed within curly braces inside of a string, it is called complex string syntax $Vegetable = "carrot"; echo " Do you have any {$Vegetable}s? ";

PHP Programming with MySQL Slide 5-14 Parsing Strings Parsing is the act of extracting characters or substrings from a larger string When programming, parsing refers to the extraction of information from string literals and variables

PHP Programming with MySQL Slide 5-15 Counting Characters and Words in a String The most commonly used string counting function is the strlen() function, which returns the total number of characters in a string Table 5-2 PHP string counting functions

PHP Programming with MySQL Slide 5-16 Counting Characters and Words in a String The str_word_count() function returns the number of words in a string Pass the str_word_count() function a literal string or the name of a string variable whose words you want to count $BookTitle = "The Cask of Amontillado"; echo " The book title contains ". str_word_count($BookTitle). “ words. ";

PHP Programming with MySQL Slide 5-17 Finding and Extracting Characters and Substrings Table 5-3 PHP string search and extraction functions

PHP Programming with MySQL Slide 5-18 Finding and Extracting Characters and Substrings Table 5-3 PHP string search and extraction functions (continued)

PHP Programming with MySQL Slide 5-19 Finding and Extracting Characters and Substrings There are two types of string search and extraction functions:  Functions that return a numeric position in a text string  Functions that return a character or substring

PHP Programming with MySQL Slide 5-20 strpos() Function Performs a case-sensitive search and returns the position of the first occurrence of one string in another string Pass two arguments to the strpos() function:  The first argument is the string you want to search  The second argument contains the characters for which you want to search If the search string is not found, the strpos() function returns a Boolean value of false

PHP Programming with MySQL Slide 5-21 strchr() and strrchr() Functions Pass to both functions the string and the character for which you want to search Both functions return a substring from the specified characters to the end of the string strchr() function starts searching at the beginning of a string strrchr() function starts searching at the end of a string

PHP Programming with MySQL Slide 5-22 substr() Function To extract characters from the beginning or middle of a string, combine the substr() function with other functions You pass to the substr() function a text string along with the starting and ending positions of the substring you want to extract $ = $NameEnd = strpos($ , echo " The name portion of the address is '“. substr($ , 0, $NameEnd). "'. ";

PHP Programming with MySQL Slide 5-23 Replacing Characters and Substrings Table 5-4 PHP string replacement functions

PHP Programming with MySQL Slide 5-24 str_replace() and str_ireplace() Functions The str_replace() and str_ireplace() functions both accept three arguments:  The string you want to search for  A replacement string  The string in which you want to replace characters $ = $New = str_replace("president", "vice.president", $ ); echo $New ; // prints

PHP Programming with MySQL Slide 5-25 Dividing Strings into Smaller Pieces Use the strtok() function to break a string into smaller strings, called tokens The syntax for the strtok() function is: $variable = strtok(string, separators); The strtok() function returns the entire string if:  An empty string is specified as the second argument of the strtok() function  The string does not contain any of the separators specified

PHP Programming with MySQL Slide 5-26 strtok() Function $Presidents = "George W. Bush;William Clinton;George H.W. Bush;Ronald Reagan;Jimmy Carter"; $President = strtok($Presidents, ";"); while ($President != NULL) { echo "$President "; $President = strtok(";"); } Figure 5-4 Output of a script that uses the strtok() function

PHP Programming with MySQL Slide 5-27 strtok() Function $Presidents = "George W. Bush;William Clinton;George H.W. Bush;Ronald Reagan;Jimmy Carter"; $President = strtok($Presidents, "; "); while ($President != NULL) { echo "$President "; $President = strtok("; "); } Figure 5-5 Output of a script with a strtok() function that uses two separators

PHP Programming with MySQL Slide 5-28 Converting Between Strings and Arrays The str_split() and explode() functions split a string into an indexed array The str_split() function splits each character in a string into an array element using the syntax: $array = str_split(string[, length]); The length argument represents the number of characters you want assigned to each array element

PHP Programming with MySQL Slide 5-29 Converting Between Strings and Arrays The explode() function splits a string into an indexed array at a specified separator The syntax for the explode() function is: $array = explode(separators, string); The order of the arguments for the explode() function is the reverse of the arguments for the strtok() function

PHP Programming with MySQL Slide 5-30 Converting Between Strings and Arrays $Presidents = "George W. Bush;William Clinton;George H.W. Bush;Ronald Reagan;Jimmy Carter"; $PresidentArray = explode(";", $Presidents); foreach ($PresidentArray as $President) { echo "$President "; } If the string does not contain the specified separators, the entire string is assigned to the first element of the array

PHP Programming with MySQL Slide 5-31 explode() Function Does not separate a string at each character that is included in the separator argument Evaluates the characters in the separator argument as a substring If you pass to the explode() function an empty string as the separator argument, the function returns a value of false

PHP Programming with MySQL Slide 5-32 implode() Function Combines an array’s elements into a single string, separated by specified characters The syntax is: $variable = implode(separators, array);

PHP Programming with MySQL Slide 5-33 implode() Function $PresidentsArray = array("George W. Bush", "William Clinton", "George H.W. Bush", "Ronald Reagan", "Jimmy Carter"); $Presidents = implode(", ", $PresidentsArray); echo $Presidents; Figure 5-6 Output of a string created with the implode() function

PHP Programming with MySQL Slide 5-34 Comparing Strings $Florida = "Miami is in Florida."; $Cuba = "Havana is in Cuba."; if ($Florida == $Cuba) echo " Same location. "; else echo " Different location. ";

PHP Programming with MySQL Slide 5-35 Comparing Strings $FirstLetter = "A"; $SecondLetter = "B"; if ($SecondLetter > $FirstLetter) echo " The second letter is higher in the alphabet than the first letter. "; else echo " The second letter is lower in the alphabet than The first letter. ";

PHP Programming with MySQL Slide 5-36 ASCII American Standard Code for Information Interchange (ASCII) Numeric representations of English characters ASCII values range from 0 to 256 Lowercase letters are represented by the values 97 (“a”) to 122 (“z”) Uppercase letters are represented by the values 65 (“A”) to 90 (“Z”) Since lowercase letters have higher values than uppercase letters, they are evaluated as being “greater” than the uppercase letters

PHP Programming with MySQL Slide 5-37 String Comparison Functions Table 5-5 PHP string comparison functions

PHP Programming with MySQL Slide 5-38 String Comparison Functions Table 5-5 PHP string comparison functions

PHP Programming with MySQL Slide 5-39 String Comparison Functions The strcasecmp() function performs a case- insensitive comparison of strings The strcmp() function performs a case-sensitive comparison of strings Both functions accept two arguments representing the strings you want to compare Most string comparison functions compare strings based on their ASCII values

PHP Programming with MySQL Slide 5-40 Determining the Similarity of Two Strings The similar_text() and levenshtein() functions are used to determine the similarity between two strings The similar_text() function returns the number of characters that two strings have in common The levenshtein() function returns the number of characters you need to change for two strings to be the same

PHP Programming with MySQL Slide 5-41 Determining the Similarity of Two Strings Both functions accept two string arguments representing the values you want to compare $FirstName = "Don"; $SecondName = "Dan"; echo " The names \"$FirstName\" and \"$SecondName\" have ". similar_text($FirstName, $SecondName). " characters in common. "; echo " You must change ". levenshtein($FirstName, $SecondName). " character(s) to make the names \"$FirstName\" and \"$SecondName\" the same. ";

PHP Programming with MySQL Slide 5-42 Determining the Similarity of Two Strings Figure 5-7 Output of a script with the similar_text() and levenshtein() functions

PHP Programming with MySQL Slide 5-43 Determining if Words Are Pronounced Similarly The soundex() and metaphone() functions determine whether two strings are pronounced similarly Both functions return a value representing how words sound The soundex() function returns a value representing a name’s phonetic equivalent The metaphone() function returns a code representing an English word’s approximate sound

PHP Programming with MySQL Slide 5-44 Determining if Words Are Pronounced Similarly $FirstName = "Gosselin"; $SecondName = "Gauselin"; $FirstNameSoundsLike = metaphone($FirstName); $SecondNameSoundsLike = metaphone($SecondName); if ($FirstNameSoundsLike == $SecondNameSoundsLike) echo " The names are pronounced the same. "; else echo " The names are not pronounced the same. ";

PHP Programming with MySQL Slide 5-45 Handling Form Submissions A query string is a set of name=value pairs appended to a target URL Form data is submitted in name=value pairs, based on the name and value attributes of each element A question mark (?) and a query string are automatically appended to the URL of a server-side script for any forms that are submitted with the GET method

PHP Programming with MySQL Slide 5-46 Handling Form Submissions Each name=value pair within the query string is separated with ampersands (&) ProcessOrder.php?favorite_books=technical&favorite_author=Gosselin

PHP Programming with MySQL Slide 5-47 Validating Submitted Data Use the isset() or empty() functions to ensure that a variable contains a value Use the is_numeric() function to test whether a variable contains a numeric string

PHP Programming with MySQL Slide 5-48 Determining if Form Variables Contain Values The isset() function determines whether a variable has been declared and initialized (or “set”) The empty() function determines whether a variable is empty Pass to both functions the name of the variable you want to check

PHP Programming with MySQL Slide 5-49 Testing if Form Variables Contain Numeric Values if (isset($_GET['height']) && isset($_GET['weight'])) { if (is_numeric($_GET['weight']) && is_numeric($_GET['height'])) { $BodyMass = $_GET['weight'] / ($_GET['height'] * $_GET['height']) * 703; printf(" Your body mass index is %d. ", $BodyMass); } else echo " You must enter numeric values! "; }

PHP Programming with MySQL Slide 5-50 Summary The concatenation operator (.) and the concatenation assignment operator (.=) can be used to combine two strings An escape character tells the compiler or interpreter that the character following the escape character has a special purpose The most commonly used string counting function is the strlen() function, which returns the total number of characters in a string

PHP Programming with MySQL Slide 5-51 Summary Use the str_replace(), str_ireplace(), and substr_replace() functions to replace text in strings The strtok() function breaks a string into smaller strings, called tokens The str_split() and explode() functions split a string into an indexed array The implode() function combines an array’s elements into a single string, separated by specified characters

PHP Programming with MySQL Slide 5-52 Summary The strcasecmp() function performs a case- insensitive comparison of strings, whereas the strcmp() function performs a case-sensitive comparison of strings The similar_text() and levenshtein() functions are used to determine the similarity of two strings The soundex() and metaphone() functions determine whether two strings are pronounced similarly