SlideShare a Scribd company logo
1 of 32
Chapter 3
Manipulating Strings
PHP Programming with MySQL
2nd
Edition
2PHP Programming with MySQL, 2nd
Edition
Objectives
In this chapter, you will:
• Construct text strings
• Work with single strings
• Work with multiple strings and parse strings
• Compare strings
• Use regular expressions
3PHP Programming with MySQL, 2nd
Edition
Constructing Text Strings
A text string contains characters surrounded by double or
single quotation marks
Text strings can be literal values:
echo "<p>PHP literal text string</p>";
…or assigned to a variable:
$StringVariable = "<p>PHP literal text string</p>";
echo $StringVariable;
4PHP Programming with MySQL, 2nd
Edition
To include a single or double quotation
marks in your displayed text:
$LatinQuote = '<p>"Et tu, Brute!"</p>';
echo $LatinQuote;
For single quotes:
$LatinQuote = "<p>'Et tu, Brute!'</p>";
echo $LatinQuote;
5PHP Programming with MySQL, 2nd
Edition
Working with String Operators
In PHP, you use two operators to combine strings:
Concatenation operator (.) combines two
strings and assigns the new value to a variable
$City = "Paris";
$Country = "France";
$Destination = <p>“ . $City . " is in "
. $Country . ".</p>";
echo $Destination;
6PHP Programming with MySQL, 2nd
Edition
Working with String Operators
• You can also combine strings using the
concatenation assignment operator (.=)
$Destination = "<p>Paris";
$Destination .= "is in France.</p>";
echo $Destination;
7PHP Programming with MySQL, 2nd
Edition
Escape Characters and Sequences
In PHP, the escape character is the backslash ()
echo '<p>This code's going to work</p>';
Do not add a backslash before an apostrophe
if you surround the text string with double
quotation marks
echo "<p>This code's going to work.</p>";
8PHP Programming with MySQL, 2nd
Edition
Escape Sequences
9PHP Programming with MySQL, 2nd
Edition
Adding Escape Characters and
Sequences (continued)
$Speaker = "Julius Caesar";
echo "<p>"Et tu, Brute!" exclaimed
$Speaker.</p>";
Figure 3-4 Output of literal text containing double quotation escape sequences
10PHP Programming with MySQL, 2nd
Edition
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 "<p>Do you have any $Vegetable?</p>";
Complex string syntax: variables placed within
curly braces inside of a string is called
$Vegetable = "carrot";
echo "<p>Do you have any {$Vegetable}s?</p>";
(more info on pages 131- 132)
11PHP Programming with MySQL, 2nd
Edition
Working with a Single String
PHP provides a number of functions for analyzing,
altering, and parsing text strings including:
– Counting characters and words
– Transposing, converting, and changing the case
of text within a string
This author likes to use a lot of
words to say something simple.
•And also bullets.
•Bullets suck….in case you were
wondering.
•I have deleted a lot them. You’re
welcome.
12
13PHP Programming with MySQL, 2nd
Edition
Counting Characters and Words
The most commonly used string counting function
is the strlen() function, which returns the total
number of characters in a string
Escape sequences, such as n, are counted as
one character
$BookTitle = "The Cask of Amontillado";
echo "<p>The book title contains " .
strlen($BookTitle) . " characters.</p>";
14PHP Programming with MySQL, 2nd
Edition
Counting Characters and Words
The str_word_count() function returns the
number of words in a string
$BookTitle = "The Cask of Amontillado";
echo "<p>The book title contains " .
str_word_count($BookTitle). " words.</p>";
15PHP Programming with MySQL, 2nd
Edition
Modifying the Case of a String
strtoupper()function - uppercase
strtolower()function - lowercase
ucfirst()function - first character is uppercase
lcfirst()function - first character is lowercase
ucwords()function - uppercase first character of
each word
16PHP Programming with MySQL, 2nd Edition
Encoding and Decoding a String
Some characters in XHTML have a special
meaning and must be encoded using HTML
entities in order to preserve that meaning
– htmlspecialchars() converts special
characters to HTML entities
– html_specialcharacters_decode()
converts HTML character entities into their
equivalent characters
17PHP Programming with MySQL, 2nd Edition
Characters Converted with htmlspecialchars()
– '&' (ampersand) to '&amp;'
– '"' (double quote) to '&quot;'
when ENT_NOQUOTES is disabled (p138)
– ''' (single quote) to '&#039;'
only when ENT_QUOTES is enabled (p138)
– '<' (less than) to '&lt;'
– '>' (greater than) to '&gt;'
18PHP Programming with MySQL, 2nd Edition
Encoding and Decoding a String
If ENT_QUOTES is enabled in the PHP
configuration, both single and double quotes are
converted
If ENT_QUOTES is disabled in the PHP
configuration, neither single nor double quotes are
converted
19PHP Programming with MySQL, 2nd Edition
The md5()function - a one-way hash
– It is a fixed-length string based on the entered
text
– It is nearly impossible to determine the
original text
– Because it is one-way, there is not equivalent
decode function
– It is used to store passwords (page 138)
20PHP Programming with MySQL, 2nd Edition
PHP provides three functions that remove
leading or trailing spaces in a string
– The trim()- removes leading or trailing spaces
– The ltrim() removes only the leading spaces
– The rtrim() removes only the trailing spaces
21PHP Programming with MySQL, 2nd Edition
The substr() returns part of a string
based on start and length parameters
$ExampleString = "woodworking project";
echo substr($ExampleString,4) . "<br />n";
echo substr($ExampleString,4,7) . "<br />n";
echo substr($ExampleString,0,8) . "<br />n";
echo substr($ExampleString,-7) . "<br />n";
echo substr($ExampleString,-12,4) . "<br />n";
(0 is the first character from the beginning)
(-1 if counting from the end.)
page (139)
22PHP Programming with MySQL, 2nd Edition
Working with Multiple Strings
Parsing is the act of dividing a string into logical
component substrings or tokens
Parsing refers to the extraction of information from
string literals and variables
23PHP Programming with MySQL, 2nd Edition
Finding Characters and Substrings
strpos()returns the position of the first
occurrence of one string in another string
strchr() searches from the beginning of a string
strrchr() searches from the end of a string
str_replace() and str_ireplace() replaces
characters within a string
strtok() breaks a string into smaller strings,
called tokens
24PHP Programming with MySQL, 2nd Edition
Dividing Strings into Smaller Pieces
$Presidents = " George Washington;John Thomas Jefferson;James
Madison;James Monroe";
$President = strtok($Presidents, ";");
while ($President != NULL) {
echo "$President<br />";
$President = strtok(";");
}
Figure 3-15 Output of a script that uses the strtok() function
25PHP Programming with MySQL, 2nd Edition
Converting Strings and Arrays
str_split() and explode() functions split a
string into an indexed array
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
26PHP Programming with MySQL, 2nd Edition
Converting Strings and Arrays
explode() 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
27PHP Programming with MySQL, 2nd Edition
Converting between Strings and
Arrays (continued)
The implode()function combines an array’s
elements into a single string, separated by specified
characters
The syntax is:
$variable = implode(separators, array);
28PHP Programming with MySQL, 2nd Edition
Converting Strings and Arrays
$PresidentsArray = array("George Washington", “John Adams",
“Thomas Jefferson", “James Madison", “James Monroe");
$Presidents = implode(", ", $PresidentsArray);
echo $Presidents;
Figure 3-18 Output of a string created with the implode() function
29PHP Programming with MySQL, 2nd Edition
Comparing Strings
Individual characters are compared by their position
in the American Standard Code for Information
Interchange (ASCII), which are numeric
representations of English characters
$FirstLetter = "A";
$SecondLetter = "B";
if ($SecondLetter > $FirstLetter)
echo "<p>The second letter is higher in the alphabet
than the first letter.</p>";
else
echo "<p>The second letter is lower in the alphabet
than
The first letter.</p>";
30PHP Programming with MySQL, 2nd Edition
Comparing Strings
American Standard Code for Information
Interchange (ASCII) values range from 0 to 255
Lowercase letters are represented by the values
97 (“a”) to 122 (“z”)
Uppercase letters are represented by the values
65 (“A”) to 90 (“Z”)
31PHP Programming with MySQL, 2nd Edition
String Comparison Functions
strcasecmp() performs a case-insensitive
comparison of strings
strcmp() performs a case-sensitive comparison
of strings
That is all folks.
32PHP Programming with MySQL, 2nd Edition

More Related Content

What's hot

Perl programming language
Perl programming languagePerl programming language
Perl programming languageElie Obeid
 
Python cheatsheet Indian Servers
Python cheatsheet Indian ServersPython cheatsheet Indian Servers
Python cheatsheet Indian ServersIndian Servers
 
101 3.7 search text files using regular expressions
101 3.7 search text files using regular expressions101 3.7 search text files using regular expressions
101 3.7 search text files using regular expressionsAcácio Oliveira
 
Introducing Modern Perl
Introducing Modern PerlIntroducing Modern Perl
Introducing Modern PerlDave Cross
 
3.7 search text files using regular expressions
3.7 search text files using regular expressions3.7 search text files using regular expressions
3.7 search text files using regular expressionsAcácio Oliveira
 
Perl 101 - The Basics of Perl Programming
Perl  101 - The Basics of Perl ProgrammingPerl  101 - The Basics of Perl Programming
Perl 101 - The Basics of Perl ProgrammingUtkarsh Sengar
 
Learning sed and awk
Learning sed and awkLearning sed and awk
Learning sed and awkYogesh Sawant
 
PHP Powerpoint -- Teach PHP with this
PHP Powerpoint -- Teach PHP with thisPHP Powerpoint -- Teach PHP with this
PHP Powerpoint -- Teach PHP with thisIan Macali
 

What's hot (20)

Intro to Perl and Bioperl
Intro to Perl and BioperlIntro to Perl and Bioperl
Intro to Perl and Bioperl
 
Perl Programming - 02 Regular Expression
Perl Programming - 02 Regular ExpressionPerl Programming - 02 Regular Expression
Perl Programming - 02 Regular Expression
 
Perl Introduction
Perl IntroductionPerl Introduction
Perl Introduction
 
Perl programming language
Perl programming languagePerl programming language
Perl programming language
 
Python cheatsheet Indian Servers
Python cheatsheet Indian ServersPython cheatsheet Indian Servers
Python cheatsheet Indian Servers
 
101 3.7 search text files using regular expressions
101 3.7 search text files using regular expressions101 3.7 search text files using regular expressions
101 3.7 search text files using regular expressions
 
Perl Basics with Examples
Perl Basics with ExamplesPerl Basics with Examples
Perl Basics with Examples
 
Perl Presentation
Perl PresentationPerl Presentation
Perl Presentation
 
Subroutines
SubroutinesSubroutines
Subroutines
 
Introducing Modern Perl
Introducing Modern PerlIntroducing Modern Perl
Introducing Modern Perl
 
3.7 search text files using regular expressions
3.7 search text files using regular expressions3.7 search text files using regular expressions
3.7 search text files using regular expressions
 
Perl 101 - The Basics of Perl Programming
Perl  101 - The Basics of Perl ProgrammingPerl  101 - The Basics of Perl Programming
Perl 101 - The Basics of Perl Programming
 
Text processing by Rj
Text processing by RjText processing by Rj
Text processing by Rj
 
Awk essentials
Awk essentialsAwk essentials
Awk essentials
 
Learning sed and awk
Learning sed and awkLearning sed and awk
Learning sed and awk
 
Introduction to Perl and BioPerl
Introduction to Perl and BioPerlIntroduction to Perl and BioPerl
Introduction to Perl and BioPerl
 
Perl tutorial
Perl tutorialPerl tutorial
Perl tutorial
 
Perl Scripting
Perl ScriptingPerl Scripting
Perl Scripting
 
PHP Powerpoint -- Teach PHP with this
PHP Powerpoint -- Teach PHP with thisPHP Powerpoint -- Teach PHP with this
PHP Powerpoint -- Teach PHP with this
 
Awk programming
Awk programming Awk programming
Awk programming
 

Viewers also liked

Synapse india reviews on php and sql
Synapse india reviews on php and sqlSynapse india reviews on php and sql
Synapse india reviews on php and sqlsaritasingh19866
 
The PHP mysqlnd plugin talk - plugins an alternative to MySQL Proxy
The PHP mysqlnd plugin talk - plugins an alternative to MySQL ProxyThe PHP mysqlnd plugin talk - plugins an alternative to MySQL Proxy
The PHP mysqlnd plugin talk - plugins an alternative to MySQL ProxyUlf Wendel
 
Php connectivitywithmysql
Php connectivitywithmysqlPhp connectivitywithmysql
Php connectivitywithmysqlBhumivaghasiya
 
10 11-hart installing pythonsoftware
10 11-hart installing pythonsoftware10 11-hart installing pythonsoftware
10 11-hart installing pythonsoftwareWilliam Hart
 
Pembahasan NETCOM Beginner Level Skill Pretest
Pembahasan NETCOM Beginner Level Skill PretestPembahasan NETCOM Beginner Level Skill Pretest
Pembahasan NETCOM Beginner Level Skill PretestI Putu Hariyadi
 
Koneksi PHP ke Database MySQL menggunakan MySQLi Extension
Koneksi PHP ke Database MySQL menggunakan MySQLi ExtensionKoneksi PHP ke Database MySQL menggunakan MySQLi Extension
Koneksi PHP ke Database MySQL menggunakan MySQLi ExtensionI Putu Hariyadi
 
Class 5 - PHP Strings
Class 5 - PHP StringsClass 5 - PHP Strings
Class 5 - PHP StringsAhmed Swilam
 
How To Install Apache, MySQL & PHP on Windows Vista
How To Install Apache, MySQL & PHP on Windows VistaHow To Install Apache, MySQL & PHP on Windows Vista
How To Install Apache, MySQL & PHP on Windows VistaMochamad Yusuf
 
Php String And Regular Expressions
Php String  And Regular ExpressionsPhp String  And Regular Expressions
Php String And Regular Expressionsmussawir20
 

Viewers also liked (20)

Synapse india reviews on php and sql
Synapse india reviews on php and sqlSynapse india reviews on php and sql
Synapse india reviews on php and sql
 
Chapter05
Chapter05Chapter05
Chapter05
 
Python lecture 10
Python lecture 10Python lecture 10
Python lecture 10
 
The PHP mysqlnd plugin talk - plugins an alternative to MySQL Proxy
The PHP mysqlnd plugin talk - plugins an alternative to MySQL ProxyThe PHP mysqlnd plugin talk - plugins an alternative to MySQL Proxy
The PHP mysqlnd plugin talk - plugins an alternative to MySQL Proxy
 
Python lecture 02
Python lecture 02Python lecture 02
Python lecture 02
 
Introduction to python
Introduction to pythonIntroduction to python
Introduction to python
 
Tt presentation.ppt
Tt presentation.pptTt presentation.ppt
Tt presentation.ppt
 
Php connectivitywithmysql
Php connectivitywithmysqlPhp connectivitywithmysql
Php connectivitywithmysql
 
10 11-hart installing pythonsoftware
10 11-hart installing pythonsoftware10 11-hart installing pythonsoftware
10 11-hart installing pythonsoftware
 
PHP Regular Expressions
PHP Regular ExpressionsPHP Regular Expressions
PHP Regular Expressions
 
Php database connectivity
Php database connectivityPhp database connectivity
Php database connectivity
 
Pembahasan NETCOM Beginner Level Skill Pretest
Pembahasan NETCOM Beginner Level Skill PretestPembahasan NETCOM Beginner Level Skill Pretest
Pembahasan NETCOM Beginner Level Skill Pretest
 
Koneksi PHP ke Database MySQL menggunakan MySQLi Extension
Koneksi PHP ke Database MySQL menggunakan MySQLi ExtensionKoneksi PHP ke Database MySQL menggunakan MySQLi Extension
Koneksi PHP ke Database MySQL menggunakan MySQLi Extension
 
DIWE - File handling with PHP
DIWE - File handling with PHPDIWE - File handling with PHP
DIWE - File handling with PHP
 
Class 5 - PHP Strings
Class 5 - PHP StringsClass 5 - PHP Strings
Class 5 - PHP Strings
 
How To Install Apache, MySQL & PHP on Windows Vista
How To Install Apache, MySQL & PHP on Windows VistaHow To Install Apache, MySQL & PHP on Windows Vista
How To Install Apache, MySQL & PHP on Windows Vista
 
Php Ppt
Php PptPhp Ppt
Php Ppt
 
Php forms
Php formsPhp forms
Php forms
 
Php String And Regular Expressions
Php String  And Regular ExpressionsPhp String  And Regular Expressions
Php String And Regular Expressions
 
Introduction to PHP
Introduction to PHPIntroduction to PHP
Introduction to PHP
 

Similar to Manipulating strings

String handling and arrays by Dr.C.R.Dhivyaa Kongu Engineering College
String handling and arrays by Dr.C.R.Dhivyaa Kongu Engineering CollegeString handling and arrays by Dr.C.R.Dhivyaa Kongu Engineering College
String handling and arrays by Dr.C.R.Dhivyaa Kongu Engineering CollegeDhivyaa C.R
 
Regular expressions in Python
Regular expressions in PythonRegular expressions in Python
Regular expressions in PythonSujith Kumar
 
String variable in php
String variable in phpString variable in php
String variable in phpchantholnet
 
Python regular expressions
Python regular expressionsPython regular expressions
Python regular expressionsKrishna Nanda
 
Regular expressions
Regular expressionsRegular expressions
Regular expressionsNicole Ryan
 
PHP and MySQL with snapshots
 PHP and MySQL with snapshots PHP and MySQL with snapshots
PHP and MySQL with snapshotsrichambra
 
Unit 2 - Regular Expression.pptx
Unit 2 - Regular Expression.pptxUnit 2 - Regular Expression.pptx
Unit 2 - Regular Expression.pptxmythili213835
 
Unit 2 - Regular Expression .pptx
Unit 2 - Regular        Expression .pptxUnit 2 - Regular        Expression .pptx
Unit 2 - Regular Expression .pptxmythili213835
 
Perl.predefined.variables
Perl.predefined.variablesPerl.predefined.variables
Perl.predefined.variablesKing Hom
 
Regular expressionfunction
Regular expressionfunctionRegular expressionfunction
Regular expressionfunctionADARSH BHATT
 
Data Analysis with R (combined slides)
Data Analysis with R (combined slides)Data Analysis with R (combined slides)
Data Analysis with R (combined slides)Guy Lebanon
 
New features in abap
New features in abapNew features in abap
New features in abapSrihari J
 
Php intro by sami kz
Php intro by sami kzPhp intro by sami kz
Php intro by sami kzsami2244
 

Similar to Manipulating strings (20)

UNIT II (7).pptx
UNIT II (7).pptxUNIT II (7).pptx
UNIT II (7).pptx
 
UNIT II (7).pptx
UNIT II (7).pptxUNIT II (7).pptx
UNIT II (7).pptx
 
String handling and arrays by Dr.C.R.Dhivyaa Kongu Engineering College
String handling and arrays by Dr.C.R.Dhivyaa Kongu Engineering CollegeString handling and arrays by Dr.C.R.Dhivyaa Kongu Engineering College
String handling and arrays by Dr.C.R.Dhivyaa Kongu Engineering College
 
Regular expressions in Python
Regular expressions in PythonRegular expressions in Python
Regular expressions in Python
 
String variable in php
String variable in phpString variable in php
String variable in php
 
lab4_php
lab4_phplab4_php
lab4_php
 
Python regular expressions
Python regular expressionsPython regular expressions
Python regular expressions
 
Variables In Php 1
Variables In Php 1Variables In Php 1
Variables In Php 1
 
Regular expressions
Regular expressionsRegular expressions
Regular expressions
 
PHP and MySQL with snapshots
 PHP and MySQL with snapshots PHP and MySQL with snapshots
PHP and MySQL with snapshots
 
Unit 2 - Regular Expression.pptx
Unit 2 - Regular Expression.pptxUnit 2 - Regular Expression.pptx
Unit 2 - Regular Expression.pptx
 
Unit 2 - Regular Expression .pptx
Unit 2 - Regular        Expression .pptxUnit 2 - Regular        Expression .pptx
Unit 2 - Regular Expression .pptx
 
Php
PhpPhp
Php
 
Perl.predefined.variables
Perl.predefined.variablesPerl.predefined.variables
Perl.predefined.variables
 
Regular expressionfunction
Regular expressionfunctionRegular expressionfunction
Regular expressionfunction
 
php_string.pdf
php_string.pdfphp_string.pdf
php_string.pdf
 
Data Analysis with R (combined slides)
Data Analysis with R (combined slides)Data Analysis with R (combined slides)
Data Analysis with R (combined slides)
 
Php
PhpPhp
Php
 
New features in abap
New features in abapNew features in abap
New features in abap
 
Php intro by sami kz
Php intro by sami kzPhp intro by sami kz
Php intro by sami kz
 

More from Nicole Ryan

Testing and Improving Performance
Testing and Improving PerformanceTesting and Improving Performance
Testing and Improving PerformanceNicole Ryan
 
Optimizing a website for search engines
Optimizing a website for search enginesOptimizing a website for search engines
Optimizing a website for search enginesNicole Ryan
 
Javascript programming using the document object model
Javascript programming using the document object modelJavascript programming using the document object model
Javascript programming using the document object modelNicole Ryan
 
Working with Video and Audio
Working with Video and AudioWorking with Video and Audio
Working with Video and AudioNicole Ryan
 
Working with Images
Working with ImagesWorking with Images
Working with ImagesNicole Ryan
 
Python Dictionaries and Sets
Python Dictionaries and SetsPython Dictionaries and Sets
Python Dictionaries and SetsNicole Ryan
 
Creating Visual Effects and Animation
Creating Visual Effects and AnimationCreating Visual Effects and Animation
Creating Visual Effects and AnimationNicole Ryan
 
Creating and Processing Web Forms
Creating and Processing Web FormsCreating and Processing Web Forms
Creating and Processing Web FormsNicole Ryan
 
Organizing Content with Lists and Tables
Organizing Content with Lists and TablesOrganizing Content with Lists and Tables
Organizing Content with Lists and TablesNicole Ryan
 
Social media and your website
Social media and your websiteSocial media and your website
Social media and your websiteNicole Ryan
 
Working with Links
Working with LinksWorking with Links
Working with LinksNicole Ryan
 
Formatting text with CSS
Formatting text with CSSFormatting text with CSS
Formatting text with CSSNicole Ryan
 
Laying Out Elements with CSS
Laying Out Elements with CSSLaying Out Elements with CSS
Laying Out Elements with CSSNicole Ryan
 
Getting Started with CSS
Getting Started with CSSGetting Started with CSS
Getting Started with CSSNicole Ryan
 
Structure Web Content
Structure Web ContentStructure Web Content
Structure Web ContentNicole Ryan
 
Getting Started with your Website
Getting Started with your WebsiteGetting Started with your Website
Getting Started with your WebsiteNicole Ryan
 
Chapter 12 Lecture: GUI Programming, Multithreading, and Animation
Chapter 12 Lecture: GUI Programming, Multithreading, and AnimationChapter 12 Lecture: GUI Programming, Multithreading, and Animation
Chapter 12 Lecture: GUI Programming, Multithreading, and AnimationNicole Ryan
 
Chapter 11: Object Oriented Programming Part 2
Chapter 11: Object Oriented Programming Part 2Chapter 11: Object Oriented Programming Part 2
Chapter 11: Object Oriented Programming Part 2Nicole Ryan
 
Intro to Programming: Modularity
Intro to Programming: ModularityIntro to Programming: Modularity
Intro to Programming: ModularityNicole Ryan
 

More from Nicole Ryan (20)

Testing and Improving Performance
Testing and Improving PerformanceTesting and Improving Performance
Testing and Improving Performance
 
Optimizing a website for search engines
Optimizing a website for search enginesOptimizing a website for search engines
Optimizing a website for search engines
 
Inheritance
InheritanceInheritance
Inheritance
 
Javascript programming using the document object model
Javascript programming using the document object modelJavascript programming using the document object model
Javascript programming using the document object model
 
Working with Video and Audio
Working with Video and AudioWorking with Video and Audio
Working with Video and Audio
 
Working with Images
Working with ImagesWorking with Images
Working with Images
 
Python Dictionaries and Sets
Python Dictionaries and SetsPython Dictionaries and Sets
Python Dictionaries and Sets
 
Creating Visual Effects and Animation
Creating Visual Effects and AnimationCreating Visual Effects and Animation
Creating Visual Effects and Animation
 
Creating and Processing Web Forms
Creating and Processing Web FormsCreating and Processing Web Forms
Creating and Processing Web Forms
 
Organizing Content with Lists and Tables
Organizing Content with Lists and TablesOrganizing Content with Lists and Tables
Organizing Content with Lists and Tables
 
Social media and your website
Social media and your websiteSocial media and your website
Social media and your website
 
Working with Links
Working with LinksWorking with Links
Working with Links
 
Formatting text with CSS
Formatting text with CSSFormatting text with CSS
Formatting text with CSS
 
Laying Out Elements with CSS
Laying Out Elements with CSSLaying Out Elements with CSS
Laying Out Elements with CSS
 
Getting Started with CSS
Getting Started with CSSGetting Started with CSS
Getting Started with CSS
 
Structure Web Content
Structure Web ContentStructure Web Content
Structure Web Content
 
Getting Started with your Website
Getting Started with your WebsiteGetting Started with your Website
Getting Started with your Website
 
Chapter 12 Lecture: GUI Programming, Multithreading, and Animation
Chapter 12 Lecture: GUI Programming, Multithreading, and AnimationChapter 12 Lecture: GUI Programming, Multithreading, and Animation
Chapter 12 Lecture: GUI Programming, Multithreading, and Animation
 
Chapter 11: Object Oriented Programming Part 2
Chapter 11: Object Oriented Programming Part 2Chapter 11: Object Oriented Programming Part 2
Chapter 11: Object Oriented Programming Part 2
 
Intro to Programming: Modularity
Intro to Programming: ModularityIntro to Programming: Modularity
Intro to Programming: Modularity
 

Recently uploaded

AUDIENCE THEORY -CULTIVATION THEORY - GERBNER.pptx
AUDIENCE THEORY -CULTIVATION THEORY -  GERBNER.pptxAUDIENCE THEORY -CULTIVATION THEORY -  GERBNER.pptx
AUDIENCE THEORY -CULTIVATION THEORY - GERBNER.pptxiammrhaywood
 
How to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERPHow to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERPCeline George
 
Full Stack Web Development Course for Beginners
Full Stack Web Development Course  for BeginnersFull Stack Web Development Course  for Beginners
Full Stack Web Development Course for BeginnersSabitha Banu
 
Choosing the Right CBSE School A Comprehensive Guide for Parents
Choosing the Right CBSE School A Comprehensive Guide for ParentsChoosing the Right CBSE School A Comprehensive Guide for Parents
Choosing the Right CBSE School A Comprehensive Guide for Parentsnavabharathschool99
 
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17Celine George
 
4.16.24 Poverty and Precarity--Desmond.pptx
4.16.24 Poverty and Precarity--Desmond.pptx4.16.24 Poverty and Precarity--Desmond.pptx
4.16.24 Poverty and Precarity--Desmond.pptxmary850239
 
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxiammrhaywood
 
4.16.24 21st Century Movements for Black Lives.pptx
4.16.24 21st Century Movements for Black Lives.pptx4.16.24 21st Century Movements for Black Lives.pptx
4.16.24 21st Century Movements for Black Lives.pptxmary850239
 
Integumentary System SMP B. Pharm Sem I.ppt
Integumentary System SMP B. Pharm Sem I.pptIntegumentary System SMP B. Pharm Sem I.ppt
Integumentary System SMP B. Pharm Sem I.pptshraddhaparab530
 
Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Celine George
 
Daily Lesson Plan in Mathematics Quarter 4
Daily Lesson Plan in Mathematics Quarter 4Daily Lesson Plan in Mathematics Quarter 4
Daily Lesson Plan in Mathematics Quarter 4JOYLYNSAMANIEGO
 
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdfGrade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdfJemuel Francisco
 
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdfInclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdfTechSoup
 
How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17Celine George
 
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptx
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptxQ4-PPT-Music9_Lesson-1-Romantic-Opera.pptx
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptxlancelewisportillo
 
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...JhezDiaz1
 
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...Nguyen Thanh Tu Collection
 
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTSGRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTSJoshuaGantuangco2
 

Recently uploaded (20)

AUDIENCE THEORY -CULTIVATION THEORY - GERBNER.pptx
AUDIENCE THEORY -CULTIVATION THEORY -  GERBNER.pptxAUDIENCE THEORY -CULTIVATION THEORY -  GERBNER.pptx
AUDIENCE THEORY -CULTIVATION THEORY - GERBNER.pptx
 
How to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERPHow to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERP
 
Full Stack Web Development Course for Beginners
Full Stack Web Development Course  for BeginnersFull Stack Web Development Course  for Beginners
Full Stack Web Development Course for Beginners
 
YOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptx
YOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptxYOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptx
YOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptx
 
Choosing the Right CBSE School A Comprehensive Guide for Parents
Choosing the Right CBSE School A Comprehensive Guide for ParentsChoosing the Right CBSE School A Comprehensive Guide for Parents
Choosing the Right CBSE School A Comprehensive Guide for Parents
 
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
 
4.16.24 Poverty and Precarity--Desmond.pptx
4.16.24 Poverty and Precarity--Desmond.pptx4.16.24 Poverty and Precarity--Desmond.pptx
4.16.24 Poverty and Precarity--Desmond.pptx
 
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
 
4.16.24 21st Century Movements for Black Lives.pptx
4.16.24 21st Century Movements for Black Lives.pptx4.16.24 21st Century Movements for Black Lives.pptx
4.16.24 21st Century Movements for Black Lives.pptx
 
Integumentary System SMP B. Pharm Sem I.ppt
Integumentary System SMP B. Pharm Sem I.pptIntegumentary System SMP B. Pharm Sem I.ppt
Integumentary System SMP B. Pharm Sem I.ppt
 
Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17
 
Daily Lesson Plan in Mathematics Quarter 4
Daily Lesson Plan in Mathematics Quarter 4Daily Lesson Plan in Mathematics Quarter 4
Daily Lesson Plan in Mathematics Quarter 4
 
YOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptx
YOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptxYOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptx
YOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptx
 
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdfGrade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
 
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdfInclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
 
How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17
 
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptx
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptxQ4-PPT-Music9_Lesson-1-Romantic-Opera.pptx
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptx
 
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
 
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
 
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTSGRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
 

Manipulating strings

  • 1. Chapter 3 Manipulating Strings PHP Programming with MySQL 2nd Edition
  • 2. 2PHP Programming with MySQL, 2nd Edition Objectives In this chapter, you will: • Construct text strings • Work with single strings • Work with multiple strings and parse strings • Compare strings • Use regular expressions
  • 3. 3PHP Programming with MySQL, 2nd Edition Constructing Text Strings A text string contains characters surrounded by double or single quotation marks Text strings can be literal values: echo "<p>PHP literal text string</p>"; …or assigned to a variable: $StringVariable = "<p>PHP literal text string</p>"; echo $StringVariable;
  • 4. 4PHP Programming with MySQL, 2nd Edition To include a single or double quotation marks in your displayed text: $LatinQuote = '<p>"Et tu, Brute!"</p>'; echo $LatinQuote; For single quotes: $LatinQuote = "<p>'Et tu, Brute!'</p>"; echo $LatinQuote;
  • 5. 5PHP Programming with MySQL, 2nd Edition Working with String Operators In PHP, you use two operators to combine strings: Concatenation operator (.) combines two strings and assigns the new value to a variable $City = "Paris"; $Country = "France"; $Destination = <p>“ . $City . " is in " . $Country . ".</p>"; echo $Destination;
  • 6. 6PHP Programming with MySQL, 2nd Edition Working with String Operators • You can also combine strings using the concatenation assignment operator (.=) $Destination = "<p>Paris"; $Destination .= "is in France.</p>"; echo $Destination;
  • 7. 7PHP Programming with MySQL, 2nd Edition Escape Characters and Sequences In PHP, the escape character is the backslash () echo '<p>This code's going to work</p>'; Do not add a backslash before an apostrophe if you surround the text string with double quotation marks echo "<p>This code's going to work.</p>";
  • 8. 8PHP Programming with MySQL, 2nd Edition Escape Sequences
  • 9. 9PHP Programming with MySQL, 2nd Edition Adding Escape Characters and Sequences (continued) $Speaker = "Julius Caesar"; echo "<p>"Et tu, Brute!" exclaimed $Speaker.</p>"; Figure 3-4 Output of literal text containing double quotation escape sequences
  • 10. 10PHP Programming with MySQL, 2nd Edition 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 "<p>Do you have any $Vegetable?</p>"; Complex string syntax: variables placed within curly braces inside of a string is called $Vegetable = "carrot"; echo "<p>Do you have any {$Vegetable}s?</p>"; (more info on pages 131- 132)
  • 11. 11PHP Programming with MySQL, 2nd Edition Working with a Single String PHP provides a number of functions for analyzing, altering, and parsing text strings including: – Counting characters and words – Transposing, converting, and changing the case of text within a string
  • 12. This author likes to use a lot of words to say something simple. •And also bullets. •Bullets suck….in case you were wondering. •I have deleted a lot them. You’re welcome. 12
  • 13. 13PHP Programming with MySQL, 2nd Edition Counting Characters and Words The most commonly used string counting function is the strlen() function, which returns the total number of characters in a string Escape sequences, such as n, are counted as one character $BookTitle = "The Cask of Amontillado"; echo "<p>The book title contains " . strlen($BookTitle) . " characters.</p>";
  • 14. 14PHP Programming with MySQL, 2nd Edition Counting Characters and Words The str_word_count() function returns the number of words in a string $BookTitle = "The Cask of Amontillado"; echo "<p>The book title contains " . str_word_count($BookTitle). " words.</p>";
  • 15. 15PHP Programming with MySQL, 2nd Edition Modifying the Case of a String strtoupper()function - uppercase strtolower()function - lowercase ucfirst()function - first character is uppercase lcfirst()function - first character is lowercase ucwords()function - uppercase first character of each word
  • 16. 16PHP Programming with MySQL, 2nd Edition Encoding and Decoding a String Some characters in XHTML have a special meaning and must be encoded using HTML entities in order to preserve that meaning – htmlspecialchars() converts special characters to HTML entities – html_specialcharacters_decode() converts HTML character entities into their equivalent characters
  • 17. 17PHP Programming with MySQL, 2nd Edition Characters Converted with htmlspecialchars() – '&' (ampersand) to '&amp;' – '"' (double quote) to '&quot;' when ENT_NOQUOTES is disabled (p138) – ''' (single quote) to '&#039;' only when ENT_QUOTES is enabled (p138) – '<' (less than) to '&lt;' – '>' (greater than) to '&gt;'
  • 18. 18PHP Programming with MySQL, 2nd Edition Encoding and Decoding a String If ENT_QUOTES is enabled in the PHP configuration, both single and double quotes are converted If ENT_QUOTES is disabled in the PHP configuration, neither single nor double quotes are converted
  • 19. 19PHP Programming with MySQL, 2nd Edition The md5()function - a one-way hash – It is a fixed-length string based on the entered text – It is nearly impossible to determine the original text – Because it is one-way, there is not equivalent decode function – It is used to store passwords (page 138)
  • 20. 20PHP Programming with MySQL, 2nd Edition PHP provides three functions that remove leading or trailing spaces in a string – The trim()- removes leading or trailing spaces – The ltrim() removes only the leading spaces – The rtrim() removes only the trailing spaces
  • 21. 21PHP Programming with MySQL, 2nd Edition The substr() returns part of a string based on start and length parameters $ExampleString = "woodworking project"; echo substr($ExampleString,4) . "<br />n"; echo substr($ExampleString,4,7) . "<br />n"; echo substr($ExampleString,0,8) . "<br />n"; echo substr($ExampleString,-7) . "<br />n"; echo substr($ExampleString,-12,4) . "<br />n"; (0 is the first character from the beginning) (-1 if counting from the end.) page (139)
  • 22. 22PHP Programming with MySQL, 2nd Edition Working with Multiple Strings Parsing is the act of dividing a string into logical component substrings or tokens Parsing refers to the extraction of information from string literals and variables
  • 23. 23PHP Programming with MySQL, 2nd Edition Finding Characters and Substrings strpos()returns the position of the first occurrence of one string in another string strchr() searches from the beginning of a string strrchr() searches from the end of a string str_replace() and str_ireplace() replaces characters within a string strtok() breaks a string into smaller strings, called tokens
  • 24. 24PHP Programming with MySQL, 2nd Edition Dividing Strings into Smaller Pieces $Presidents = " George Washington;John Thomas Jefferson;James Madison;James Monroe"; $President = strtok($Presidents, ";"); while ($President != NULL) { echo "$President<br />"; $President = strtok(";"); } Figure 3-15 Output of a script that uses the strtok() function
  • 25. 25PHP Programming with MySQL, 2nd Edition Converting Strings and Arrays str_split() and explode() functions split a string into an indexed array 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
  • 26. 26PHP Programming with MySQL, 2nd Edition Converting Strings and Arrays explode() 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
  • 27. 27PHP Programming with MySQL, 2nd Edition Converting between Strings and Arrays (continued) The implode()function combines an array’s elements into a single string, separated by specified characters The syntax is: $variable = implode(separators, array);
  • 28. 28PHP Programming with MySQL, 2nd Edition Converting Strings and Arrays $PresidentsArray = array("George Washington", “John Adams", “Thomas Jefferson", “James Madison", “James Monroe"); $Presidents = implode(", ", $PresidentsArray); echo $Presidents; Figure 3-18 Output of a string created with the implode() function
  • 29. 29PHP Programming with MySQL, 2nd Edition Comparing Strings Individual characters are compared by their position in the American Standard Code for Information Interchange (ASCII), which are numeric representations of English characters $FirstLetter = "A"; $SecondLetter = "B"; if ($SecondLetter > $FirstLetter) echo "<p>The second letter is higher in the alphabet than the first letter.</p>"; else echo "<p>The second letter is lower in the alphabet than The first letter.</p>";
  • 30. 30PHP Programming with MySQL, 2nd Edition Comparing Strings American Standard Code for Information Interchange (ASCII) values range from 0 to 255 Lowercase letters are represented by the values 97 (“a”) to 122 (“z”) Uppercase letters are represented by the values 65 (“A”) to 90 (“Z”)
  • 31. 31PHP Programming with MySQL, 2nd Edition String Comparison Functions strcasecmp() performs a case-insensitive comparison of strings strcmp() performs a case-sensitive comparison of strings
  • 32. That is all folks. 32PHP Programming with MySQL, 2nd Edition