SlideShare a Scribd company logo
Strings
TYBSc. Comp. Sci
Chapter 2- Functions & Strings
monica deshmane(H.V.Desai college) 1
Points to learn
String
• Encoding and escaping
• Removing HTML tags
• Comparing strings
monica deshmane(H.V.Desai college) 2
Encoding and Escaping
HTML, web page addresses, and database
commands are all strings, but they each require
different characters to be escaped in different ways.
PHP has a number of built-in functions to convert to
and from these encodings.
these encodings are placed between & and ;
“ encoded to &quote;
< encoded to &lt;
> encoded to &gt;
‘ encoded to &#039;
& encoded to &amp; monica deshmane(H.V.Desai college) 3
2 functions
1. Entity-quoting all special characters
• htmlentities(string,[quotestyle,character-
set])
2.Entity-quoting only HTML syntax
characters
• Htmlspecialchars
(string,[quotestyle,character-set])
monica deshmane(H.V.Desai college) 4
Encoding and Escaping
• Entity-quoting all special characters
Ex. htmlentities($str, ENT_COMPAT);
ENT_COMPAT - convert double-quotes and leave
single-quotes as it is. This is bydefault
ENT_QUOTES - convert both double and single
quotes.
ENT_NOQUOTES - leave both double and single
quotes as it is.
ENT_IGNORE - Silently discard invalid code unit
sequences instead of returning an empty string.
monica deshmane(H.V.Desai college) 5
Entity-quoting only HTML syntax
characters
• $str = "<html><body>John &
'Jim'</body></html>";
• echo htmlspecialchars($str, ENT_COMPAT);
• echo "<br />";
• echo htmlspecialchars($str, ENT_QUOTES);
• echo "<br />";
• echo htmlspecialchars($str, ENT_NOQUOTES);
• The browser output of the code above will be:
• <html><body>John & 'Jim'</body></html>
• <html><body>John & 'Jim'</body></html>
• <html><body>John & 'Jim'</body></html>monica deshmane(H.V.Desai college) 6
Encoding and Escaping
$str = "John & 'Jim'";
echo htmlentities($str, ENT_COMPAT);
echo "<br />";
echo htmlentities($str, ENT_QUOTES);
echo "<br />";
echo htmlentities($str, ENT_NOQUOTES);
The browser output of the code above will be:
John & 'Jim'
John & 'Jim'
John & 'Jim'
Select "View source" in the browser window:
John &amp; 'Jim'<br />
John &amp; &#039; Jim &#039;<br />
John &amp; 'Jim'
monica deshmane(H.V.Desai college) 7
Entity-quoting only HTML syntax
characters
• Select "View source" in the browser
window:
&lt;html&gt;&lt;body&gt;John &amp;
'Jim'&lt;/body&gt;&lt;/html&gt;<br />
&lt;html&gt;&lt;body&gt;John &amp;
&#039;Jim&#039;&lt;/body&gt;&lt;/html&gt;<br />
&lt;html&gt;&lt;body&gt;John &amp;
'Jim'&lt;/body&gt;&lt;/html&gt; monica deshmane(H.V.Desai college) 8
Entity-quoting only HTML syntax
characters
• htmlspecialchars(string,quotestyle,character-set)
The htmlspecialchars() function converts some
• predefined characters to HTML entities.
• The predefined characters are:
• & (ampersand) becomes &amp;
• " (double quote) becomes &quot;
• ' (single quote) becomes &#039;
• < (less than) becomes &lt;
• > (greater than) becomes &gt;
monica deshmane(H.V.Desai college)
9
Removing HTML tags
• Function-:
• strip_tags()
• What we require?
• String
• Which tags to allow?
• Syntax-
• strip_tags(string[, allow])
monica deshmane(H.V.Desai college) 10
Removing HTML tags
strip_tags()
The strip_tags() function strips a string from HTML,
XML, and PHP tags.
strip_tags(string[, allow])
First parameter is the string to check,
allow is Optional that Specifies allowable tags. These
tags will not be removed.
echo strip_tags("Hello <b><i>world!</i></b>");
Output: Hello world
echo strip_tags("Hello <b><i>world!</i></b>", "<b>");
Output: Hello world
monica deshmane(H.V.Desai college) 11
monica deshmane(H.V.Desai college) 12
add slashes
echo addslashes( “it’s interesting” );
Output:
“it’s interesting”
remove slashes
echo stripslashes(“it’s interesting”);
Output:
“it’s interesting”
monica deshmane(H.V.Desai college) 13
–what require to compare 2 strings?
Str1
Str2
Syntax- $diff = strcmp($str1, $str2);
This function is used for comparing two strings. It returns a number less than 0 if str1 is
less than str2, returns greater than 0 if str1 grater than str2 and return 0 if str1 = str2
$str1= “hello";
$str2 = “HELLO";
$diff = strcmp($str1, $str2);
echo "$diff";
Output:
1
Comparing Strings
1. strcmp()
monica deshmane(H.V.Desai college) 14
Comparing Strings
2. strcasecmp()
2. strcasecmp() –
What we require?
Syntax- $diff = strcasecmp($str1, $str2);
This function converts string to lower case then compares the value.
So output will be 0.
monica deshmane(H.V.Desai college) 15
What we require?
Str1
Str2
No.of letters n to compare
Syntax- $comp= strncmp(str1, str2,n);
$str1= “hello";
$str2 = “heLLO";
$comp= strcmp($str1, $str2,2);
echo "$comp"; //true
4.Strncasecmp //same as strncmp()
Comparing Strings
3. Strncmp()
monica deshmane(H.V.Desai college) 16
1) soundex()
2) metaphone()
Syntax –
if (soundex($str) == soundex($str2)){}
if (metaphone($str) == metaphone($str2)){}
Comparing Strings 5. Approximate equality
monica deshmane(H.V.Desai college) 17
1) soundex() - Soundex keys produce the same soundex key according to pronunciation,
and can thus be used to simplify searches in databases where you know the
pronunciation but not the spelling.
$str = “weather";//know, our, son
$str2 = “whether"; //no, hour, sun
if (soundex($str) == soundex($str2))
{ echo "Similar";
}
else
{ echo "Not similar";
}
//Similar
Comparing Strings 5. Approximate equality
monica deshmane(H.V.Desai college) 18
2)metaphone() - Similar to soundex() metaphone creates the same key for similar sounding
words. It's more accurate than soundex() as it knows the basic rules of English pronunciation.
The metaphone generated keys are of variable length.
$str = “weather";//know, our, son
$str2 = “whether"; //no, hour, sun
if (metaphone($str) == metaphone($str2))
{ echo "Similar";
}
else
{
echo "Not similar";
}
//Not Similar
Comparing Strings 5. Approximate equality
monica deshmane(H.V.Desai college) 19
- function returns the number of matching characters of two strings. This function also
calculate the similarity of the two strings in percent.
What we require?
Str1
Str2
Percent of similarity
Syntax-:
similar_text(str1,str2,percent);
Comparing Strings 6. similar_text()
monica deshmane(H.V.Desai college) 20
echo "Number of character matching : ".similar_text("Hello World","Hello Welcome",
$percent);
echo "<br>Percentage : ".$percent;
Output:
Number of character matching : 8
Percentage : 66.666666666667
Comparing Strings 6. similar_text()
monica deshmane(H.V.Desai college) 21
- function returns the Levenshtein distance between two strings.
The Levenshtein distance is the number of characters you have to replace, insert or delete to
convert string1 into string2.
What we require?
Str1,
Str2
Syntax-
$diff= levenshtein(str1,str2);
Ex.
echo levenshtein("Hello World","ello World");
//1
Comparing Strings 7. levenshtein()
monica deshmane(H.V.Desai college) 22
Revise string
comparison functions-
1. Strcmp()
2. Strcasecmp()
3. Strncmp()
4. Strncasecmp()
5. Approximate equality
1. soundex()
2. metaphone()
6. similar_text()
7. levenshtein ()
End of Presentation
monica deshmane(H.V.Desai college) 23

More Related Content

What's hot (20)

JLIFF: Where we are, and where we're going
JLIFF: Where we are, and where we're goingJLIFF: Where we are, and where we're going
JLIFF: Where we are, and where we're going
Chase Tingley
 
Ruby quick ref
Ruby quick refRuby quick ref
Ruby quick ref
Tharcius Silva
 
Ruby cheat sheet
Ruby cheat sheetRuby cheat sheet
Ruby cheat sheet
Tharcius Silva
 
Class 5 - PHP Strings
Class 5 - PHP StringsClass 5 - PHP Strings
Class 5 - PHP Strings
Ahmed Swilam
 
Introduction to PHP Lecture 1
Introduction to PHP Lecture 1Introduction to PHP Lecture 1
Introduction to PHP Lecture 1
Ajay Khatri
 
/Regex makes me want to (weep|give up|(╯°□°)╯︵ ┻━┻)\.?/i
/Regex makes me want to (weep|give up|(╯°□°)╯︵ ┻━┻)\.?/i/Regex makes me want to (weep|give up|(╯°□°)╯︵ ┻━┻)\.?/i
/Regex makes me want to (weep|give up|(╯°□°)╯︵ ┻━┻)\.?/i
brettflorio
 
Basic perl programming
Basic perl programmingBasic perl programming
Basic perl programming
Thang Nguyen
 
Schema design short
Schema design shortSchema design short
Schema design short
MongoDB
 
Working with text, Regular expressions
Working with text, Regular expressionsWorking with text, Regular expressions
Working with text, Regular expressions
Krasimir Berov (Красимир Беров)
 
BabelJS - James Kyle at Modern Web UI
BabelJS - James Kyle at Modern Web UIBabelJS - James Kyle at Modern Web UI
BabelJS - James Kyle at Modern Web UI
modernweb
 
Jquery presentation
Jquery presentationJquery presentation
Jquery presentation
guest5d87aa6
 
Basics of Java Script (JS)
Basics of Java Script (JS)Basics of Java Script (JS)
Basics of Java Script (JS)
Ajay Khatri
 
Perl.Hacks.On.Vim
Perl.Hacks.On.VimPerl.Hacks.On.Vim
Perl.Hacks.On.Vim
Lin Yo-An
 
Ruby Topic Maps Tutorial (2007-10-10)
Ruby Topic Maps Tutorial (2007-10-10)Ruby Topic Maps Tutorial (2007-10-10)
Ruby Topic Maps Tutorial (2007-10-10)
Benjamin Bock
 
Good Evils In Perl
Good Evils In PerlGood Evils In Perl
Good Evils In Perl
Kang-min Liu
 
Dealing with Legacy Perl Code - Peter Scott
Dealing with Legacy Perl Code - Peter ScottDealing with Legacy Perl Code - Peter Scott
Dealing with Legacy Perl Code - Peter Scott
O'Reilly Media
 
Php Crash Course - Macq Electronique 2010
Php Crash Course - Macq Electronique 2010Php Crash Course - Macq Electronique 2010
Php Crash Course - Macq Electronique 2010
Michelangelo van Dam
 
PHP Strings and Patterns
PHP Strings and PatternsPHP Strings and Patterns
PHP Strings and Patterns
Henry Osborne
 
Using DAOs without implementing them
Using DAOs without implementing themUsing DAOs without implementing them
Using DAOs without implementing them
benfante
 
PHP Powerpoint -- Teach PHP with this
PHP Powerpoint -- Teach PHP with thisPHP Powerpoint -- Teach PHP with this
PHP Powerpoint -- Teach PHP with this
Ian Macali
 
JLIFF: Where we are, and where we're going
JLIFF: Where we are, and where we're goingJLIFF: Where we are, and where we're going
JLIFF: Where we are, and where we're going
Chase Tingley
 
Class 5 - PHP Strings
Class 5 - PHP StringsClass 5 - PHP Strings
Class 5 - PHP Strings
Ahmed Swilam
 
Introduction to PHP Lecture 1
Introduction to PHP Lecture 1Introduction to PHP Lecture 1
Introduction to PHP Lecture 1
Ajay Khatri
 
/Regex makes me want to (weep|give up|(╯°□°)╯︵ ┻━┻)\.?/i
/Regex makes me want to (weep|give up|(╯°□°)╯︵ ┻━┻)\.?/i/Regex makes me want to (weep|give up|(╯°□°)╯︵ ┻━┻)\.?/i
/Regex makes me want to (weep|give up|(╯°□°)╯︵ ┻━┻)\.?/i
brettflorio
 
Basic perl programming
Basic perl programmingBasic perl programming
Basic perl programming
Thang Nguyen
 
Schema design short
Schema design shortSchema design short
Schema design short
MongoDB
 
BabelJS - James Kyle at Modern Web UI
BabelJS - James Kyle at Modern Web UIBabelJS - James Kyle at Modern Web UI
BabelJS - James Kyle at Modern Web UI
modernweb
 
Jquery presentation
Jquery presentationJquery presentation
Jquery presentation
guest5d87aa6
 
Basics of Java Script (JS)
Basics of Java Script (JS)Basics of Java Script (JS)
Basics of Java Script (JS)
Ajay Khatri
 
Perl.Hacks.On.Vim
Perl.Hacks.On.VimPerl.Hacks.On.Vim
Perl.Hacks.On.Vim
Lin Yo-An
 
Ruby Topic Maps Tutorial (2007-10-10)
Ruby Topic Maps Tutorial (2007-10-10)Ruby Topic Maps Tutorial (2007-10-10)
Ruby Topic Maps Tutorial (2007-10-10)
Benjamin Bock
 
Good Evils In Perl
Good Evils In PerlGood Evils In Perl
Good Evils In Perl
Kang-min Liu
 
Dealing with Legacy Perl Code - Peter Scott
Dealing with Legacy Perl Code - Peter ScottDealing with Legacy Perl Code - Peter Scott
Dealing with Legacy Perl Code - Peter Scott
O'Reilly Media
 
Php Crash Course - Macq Electronique 2010
Php Crash Course - Macq Electronique 2010Php Crash Course - Macq Electronique 2010
Php Crash Course - Macq Electronique 2010
Michelangelo van Dam
 
PHP Strings and Patterns
PHP Strings and PatternsPHP Strings and Patterns
PHP Strings and Patterns
Henry Osborne
 
Using DAOs without implementing them
Using DAOs without implementing themUsing DAOs without implementing them
Using DAOs without implementing them
benfante
 
PHP Powerpoint -- Teach PHP with this
PHP Powerpoint -- Teach PHP with thisPHP Powerpoint -- Teach PHP with this
PHP Powerpoint -- Teach PHP with this
Ian Macali
 

Similar to php string-part 2 (20)

Php and MySQL
Php and MySQLPhp and MySQL
Php and MySQL
Tiji Thomas
 
Csharp4 strings and_regular_expressions
Csharp4 strings and_regular_expressionsCsharp4 strings and_regular_expressions
Csharp4 strings and_regular_expressions
Abed Bukhari
 
Ch1(introduction to php)
Ch1(introduction to php)Ch1(introduction to php)
Ch1(introduction to php)
Chhom Karath
 
First steps in C-Shell
First steps in C-ShellFirst steps in C-Shell
First steps in C-Shell
Brahma Killampalli
 
The Rust Borrow Checker
The Rust Borrow CheckerThe Rust Borrow Checker
The Rust Borrow Checker
Nell Shamrell-Harrington
 
Web Application Development using PHP Chapter 3
Web Application Development using PHP Chapter 3Web Application Development using PHP Chapter 3
Web Application Development using PHP Chapter 3
Mohd Harris Ahmad Jaal
 
My First Rails Plugin - Usertext
My First Rails Plugin - UsertextMy First Rails Plugin - Usertext
My First Rails Plugin - Usertext
frankieroberto
 
Text based search engine on a fixed corpus and utilizing indexation and ranki...
Text based search engine on a fixed corpus and utilizing indexation and ranki...Text based search engine on a fixed corpus and utilizing indexation and ranki...
Text based search engine on a fixed corpus and utilizing indexation and ranki...
Soham Mondal
 
Domain Specific Languages (EclipseCon 2012)
Domain Specific Languages (EclipseCon 2012)Domain Specific Languages (EclipseCon 2012)
Domain Specific Languages (EclipseCon 2012)
Sven Efftinge
 
PHP Introduction and Training Material
PHP Introduction and Training MaterialPHP Introduction and Training Material
PHP Introduction and Training Material
Manoj kumar
 
Bioinformatica: Esercizi su Perl, espressioni regolari e altre amenità (BMR G...
Bioinformatica: Esercizi su Perl, espressioni regolari e altre amenità (BMR G...Bioinformatica: Esercizi su Perl, espressioni regolari e altre amenità (BMR G...
Bioinformatica: Esercizi su Perl, espressioni regolari e altre amenità (BMR G...
Andrea Telatin
 
ES6 is Nigh
ES6 is NighES6 is Nigh
ES6 is Nigh
Domenic Denicola
 
Building an e:commerce site with PHP
Building an e:commerce site with PHPBuilding an e:commerce site with PHP
Building an e:commerce site with PHP
webhostingguy
 
J Query Public
J Query PublicJ Query Public
J Query Public
pradeepsilamkoti
 
PHP complete reference with database concepts for beginners
PHP complete reference with database concepts for beginnersPHP complete reference with database concepts for beginners
PHP complete reference with database concepts for beginners
Mohammed Mushtaq Ahmed
 
Attacks against Microsoft network web clients
Attacks against Microsoft network web clients Attacks against Microsoft network web clients
Attacks against Microsoft network web clients
Positive Hack Days
 
Clojure: Simple By Design
Clojure: Simple By DesignClojure: Simple By Design
Clojure: Simple By Design
All Things Open
 
lab4_php
lab4_phplab4_php
lab4_php
tutorialsruby
 
lab4_php
lab4_phplab4_php
lab4_php
tutorialsruby
 
Modern, Scalable, Ambitious apps with Ember.js
Modern, Scalable, Ambitious apps with Ember.jsModern, Scalable, Ambitious apps with Ember.js
Modern, Scalable, Ambitious apps with Ember.js
Mike North
 
Csharp4 strings and_regular_expressions
Csharp4 strings and_regular_expressionsCsharp4 strings and_regular_expressions
Csharp4 strings and_regular_expressions
Abed Bukhari
 
Ch1(introduction to php)
Ch1(introduction to php)Ch1(introduction to php)
Ch1(introduction to php)
Chhom Karath
 
Web Application Development using PHP Chapter 3
Web Application Development using PHP Chapter 3Web Application Development using PHP Chapter 3
Web Application Development using PHP Chapter 3
Mohd Harris Ahmad Jaal
 
My First Rails Plugin - Usertext
My First Rails Plugin - UsertextMy First Rails Plugin - Usertext
My First Rails Plugin - Usertext
frankieroberto
 
Text based search engine on a fixed corpus and utilizing indexation and ranki...
Text based search engine on a fixed corpus and utilizing indexation and ranki...Text based search engine on a fixed corpus and utilizing indexation and ranki...
Text based search engine on a fixed corpus and utilizing indexation and ranki...
Soham Mondal
 
Domain Specific Languages (EclipseCon 2012)
Domain Specific Languages (EclipseCon 2012)Domain Specific Languages (EclipseCon 2012)
Domain Specific Languages (EclipseCon 2012)
Sven Efftinge
 
PHP Introduction and Training Material
PHP Introduction and Training MaterialPHP Introduction and Training Material
PHP Introduction and Training Material
Manoj kumar
 
Bioinformatica: Esercizi su Perl, espressioni regolari e altre amenità (BMR G...
Bioinformatica: Esercizi su Perl, espressioni regolari e altre amenità (BMR G...Bioinformatica: Esercizi su Perl, espressioni regolari e altre amenità (BMR G...
Bioinformatica: Esercizi su Perl, espressioni regolari e altre amenità (BMR G...
Andrea Telatin
 
Building an e:commerce site with PHP
Building an e:commerce site with PHPBuilding an e:commerce site with PHP
Building an e:commerce site with PHP
webhostingguy
 
PHP complete reference with database concepts for beginners
PHP complete reference with database concepts for beginnersPHP complete reference with database concepts for beginners
PHP complete reference with database concepts for beginners
Mohammed Mushtaq Ahmed
 
Attacks against Microsoft network web clients
Attacks against Microsoft network web clients Attacks against Microsoft network web clients
Attacks against Microsoft network web clients
Positive Hack Days
 
Clojure: Simple By Design
Clojure: Simple By DesignClojure: Simple By Design
Clojure: Simple By Design
All Things Open
 
Modern, Scalable, Ambitious apps with Ember.js
Modern, Scalable, Ambitious apps with Ember.jsModern, Scalable, Ambitious apps with Ember.js
Modern, Scalable, Ambitious apps with Ember.js
Mike North
 

More from monikadeshmane (20)

File system node js
File system node jsFile system node js
File system node js
monikadeshmane
 
Nodejs functions & modules
Nodejs functions & modulesNodejs functions & modules
Nodejs functions & modules
monikadeshmane
 
Nodejs buffers
Nodejs buffersNodejs buffers
Nodejs buffers
monikadeshmane
 
Intsllation & 1st program nodejs
Intsllation & 1st program nodejsIntsllation & 1st program nodejs
Intsllation & 1st program nodejs
monikadeshmane
 
Nodejs basics
Nodejs basicsNodejs basics
Nodejs basics
monikadeshmane
 
Chap 5 php files part-2
Chap 5 php files   part-2Chap 5 php files   part-2
Chap 5 php files part-2
monikadeshmane
 
Chap 5 php files part 1
Chap 5 php files part 1Chap 5 php files part 1
Chap 5 php files part 1
monikadeshmane
 
Chap4 oop class (php) part 2
Chap4 oop class (php) part 2Chap4 oop class (php) part 2
Chap4 oop class (php) part 2
monikadeshmane
 
Chap4 oop class (php) part 1
Chap4 oop class (php) part 1Chap4 oop class (php) part 1
Chap4 oop class (php) part 1
monikadeshmane
 
Chap 3php array part4
Chap 3php array part4Chap 3php array part4
Chap 3php array part4
monikadeshmane
 
Chap 3php array part 3
Chap 3php array part 3Chap 3php array part 3
Chap 3php array part 3
monikadeshmane
 
Chap 3php array part 2
Chap 3php array part 2Chap 3php array part 2
Chap 3php array part 2
monikadeshmane
 
Chap 3php array part1
Chap 3php array part1Chap 3php array part1
Chap 3php array part1
monikadeshmane
 
PHP function
PHP functionPHP function
PHP function
monikadeshmane
 
php string part 4
php string part 4php string part 4
php string part 4
monikadeshmane
 
php string part 3
php string part 3php string part 3
php string part 3
monikadeshmane
 
PHP string-part 1
PHP string-part 1PHP string-part 1
PHP string-part 1
monikadeshmane
 
java script
java scriptjava script
java script
monikadeshmane
 
ip1clientserver model
 ip1clientserver model ip1clientserver model
ip1clientserver model
monikadeshmane
 
Chap1introppt2php(finally done)
Chap1introppt2php(finally done)Chap1introppt2php(finally done)
Chap1introppt2php(finally done)
monikadeshmane
 

Recently uploaded (20)

Presentation of the MIPLM subject matter expert Erdem Kaya
Presentation of the MIPLM subject matter expert Erdem KayaPresentation of the MIPLM subject matter expert Erdem Kaya
Presentation of the MIPLM subject matter expert Erdem Kaya
MIPLM
 
New Microsoft PowerPoint Presentation.pptx
New Microsoft PowerPoint Presentation.pptxNew Microsoft PowerPoint Presentation.pptx
New Microsoft PowerPoint Presentation.pptx
milanasargsyan5
 
Anti-Depressants pharmacology 1slide.pptx
Anti-Depressants pharmacology 1slide.pptxAnti-Depressants pharmacology 1slide.pptx
Anti-Depressants pharmacology 1slide.pptx
Mayuri Chavan
 
Sinhala_Male_Names.pdf Sinhala_Male_Name
Sinhala_Male_Names.pdf Sinhala_Male_NameSinhala_Male_Names.pdf Sinhala_Male_Name
Sinhala_Male_Names.pdf Sinhala_Male_Name
keshanf79
 
How to track Cost and Revenue using Analytic Accounts in odoo Accounting, App...
How to track Cost and Revenue using Analytic Accounts in odoo Accounting, App...How to track Cost and Revenue using Analytic Accounts in odoo Accounting, App...
How to track Cost and Revenue using Analytic Accounts in odoo Accounting, App...
Celine George
 
Biophysics Chapter 3 Methods of Studying Macromolecules.pdf
Biophysics Chapter 3 Methods of Studying Macromolecules.pdfBiophysics Chapter 3 Methods of Studying Macromolecules.pdf
Biophysics Chapter 3 Methods of Studying Macromolecules.pdf
PKLI-Institute of Nursing and Allied Health Sciences Lahore , Pakistan.
 
pulse ppt.pptx Types of pulse , characteristics of pulse , Alteration of pulse
pulse  ppt.pptx Types of pulse , characteristics of pulse , Alteration of pulsepulse  ppt.pptx Types of pulse , characteristics of pulse , Alteration of pulse
pulse ppt.pptx Types of pulse , characteristics of pulse , Alteration of pulse
sushreesangita003
 
How to Subscribe Newsletter From Odoo 18 Website
How to Subscribe Newsletter From Odoo 18 WebsiteHow to Subscribe Newsletter From Odoo 18 Website
How to Subscribe Newsletter From Odoo 18 Website
Celine George
 
Odoo Inventory Rules and Routes v17 - Odoo Slides
Odoo Inventory Rules and Routes v17 - Odoo SlidesOdoo Inventory Rules and Routes v17 - Odoo Slides
Odoo Inventory Rules and Routes v17 - Odoo Slides
Celine George
 
2541William_McCollough_DigitalDetox.docx
2541William_McCollough_DigitalDetox.docx2541William_McCollough_DigitalDetox.docx
2541William_McCollough_DigitalDetox.docx
contactwilliamm2546
 
Handling Multiple Choice Responses: Fortune Effiong.pptx
Handling Multiple Choice Responses: Fortune Effiong.pptxHandling Multiple Choice Responses: Fortune Effiong.pptx
Handling Multiple Choice Responses: Fortune Effiong.pptx
AuthorAIDNationalRes
 
Metamorphosis: Life's Transformative Journey
Metamorphosis: Life's Transformative JourneyMetamorphosis: Life's Transformative Journey
Metamorphosis: Life's Transformative Journey
Arshad Shaikh
 
How to manage Multiple Warehouses for multiple floors in odoo point of sale
How to manage Multiple Warehouses for multiple floors in odoo point of saleHow to manage Multiple Warehouses for multiple floors in odoo point of sale
How to manage Multiple Warehouses for multiple floors in odoo point of sale
Celine George
 
Marie Boran Special Collections Librarian Hardiman Library, University of Gal...
Marie Boran Special Collections Librarian Hardiman Library, University of Gal...Marie Boran Special Collections Librarian Hardiman Library, University of Gal...
Marie Boran Special Collections Librarian Hardiman Library, University of Gal...
Library Association of Ireland
 
Political History of Pala dynasty Pala Rulers NEP.pptx
Political History of Pala dynasty Pala Rulers NEP.pptxPolitical History of Pala dynasty Pala Rulers NEP.pptx
Political History of Pala dynasty Pala Rulers NEP.pptx
Arya Mahila P. G. College, Banaras Hindu University, Varanasi, India.
 
World war-1(Causes & impacts at a glance) PPT by Simanchala Sarab(BABed,sem-4...
World war-1(Causes & impacts at a glance) PPT by Simanchala Sarab(BABed,sem-4...World war-1(Causes & impacts at a glance) PPT by Simanchala Sarab(BABed,sem-4...
World war-1(Causes & impacts at a glance) PPT by Simanchala Sarab(BABed,sem-4...
larencebapu132
 
Geography Sem II Unit 1C Correlation of Geography with other school subjects
Geography Sem II Unit 1C Correlation of Geography with other school subjectsGeography Sem II Unit 1C Correlation of Geography with other school subjects
Geography Sem II Unit 1C Correlation of Geography with other school subjects
ProfDrShaikhImran
 
How to Set warnings for invoicing specific customers in odoo
How to Set warnings for invoicing specific customers in odooHow to Set warnings for invoicing specific customers in odoo
How to Set warnings for invoicing specific customers in odoo
Celine George
 
SCI BIZ TECH QUIZ (OPEN) PRELIMS XTASY 2025.pptx
SCI BIZ TECH QUIZ (OPEN) PRELIMS XTASY 2025.pptxSCI BIZ TECH QUIZ (OPEN) PRELIMS XTASY 2025.pptx
SCI BIZ TECH QUIZ (OPEN) PRELIMS XTASY 2025.pptx
Ronisha Das
 
YSPH VMOC Special Report - Measles Outbreak Southwest US 4-30-2025.pptx
YSPH VMOC Special Report - Measles Outbreak  Southwest US 4-30-2025.pptxYSPH VMOC Special Report - Measles Outbreak  Southwest US 4-30-2025.pptx
YSPH VMOC Special Report - Measles Outbreak Southwest US 4-30-2025.pptx
Yale School of Public Health - The Virtual Medical Operations Center (VMOC)
 
Presentation of the MIPLM subject matter expert Erdem Kaya
Presentation of the MIPLM subject matter expert Erdem KayaPresentation of the MIPLM subject matter expert Erdem Kaya
Presentation of the MIPLM subject matter expert Erdem Kaya
MIPLM
 
New Microsoft PowerPoint Presentation.pptx
New Microsoft PowerPoint Presentation.pptxNew Microsoft PowerPoint Presentation.pptx
New Microsoft PowerPoint Presentation.pptx
milanasargsyan5
 
Anti-Depressants pharmacology 1slide.pptx
Anti-Depressants pharmacology 1slide.pptxAnti-Depressants pharmacology 1slide.pptx
Anti-Depressants pharmacology 1slide.pptx
Mayuri Chavan
 
Sinhala_Male_Names.pdf Sinhala_Male_Name
Sinhala_Male_Names.pdf Sinhala_Male_NameSinhala_Male_Names.pdf Sinhala_Male_Name
Sinhala_Male_Names.pdf Sinhala_Male_Name
keshanf79
 
How to track Cost and Revenue using Analytic Accounts in odoo Accounting, App...
How to track Cost and Revenue using Analytic Accounts in odoo Accounting, App...How to track Cost and Revenue using Analytic Accounts in odoo Accounting, App...
How to track Cost and Revenue using Analytic Accounts in odoo Accounting, App...
Celine George
 
pulse ppt.pptx Types of pulse , characteristics of pulse , Alteration of pulse
pulse  ppt.pptx Types of pulse , characteristics of pulse , Alteration of pulsepulse  ppt.pptx Types of pulse , characteristics of pulse , Alteration of pulse
pulse ppt.pptx Types of pulse , characteristics of pulse , Alteration of pulse
sushreesangita003
 
How to Subscribe Newsletter From Odoo 18 Website
How to Subscribe Newsletter From Odoo 18 WebsiteHow to Subscribe Newsletter From Odoo 18 Website
How to Subscribe Newsletter From Odoo 18 Website
Celine George
 
Odoo Inventory Rules and Routes v17 - Odoo Slides
Odoo Inventory Rules and Routes v17 - Odoo SlidesOdoo Inventory Rules and Routes v17 - Odoo Slides
Odoo Inventory Rules and Routes v17 - Odoo Slides
Celine George
 
2541William_McCollough_DigitalDetox.docx
2541William_McCollough_DigitalDetox.docx2541William_McCollough_DigitalDetox.docx
2541William_McCollough_DigitalDetox.docx
contactwilliamm2546
 
Handling Multiple Choice Responses: Fortune Effiong.pptx
Handling Multiple Choice Responses: Fortune Effiong.pptxHandling Multiple Choice Responses: Fortune Effiong.pptx
Handling Multiple Choice Responses: Fortune Effiong.pptx
AuthorAIDNationalRes
 
Metamorphosis: Life's Transformative Journey
Metamorphosis: Life's Transformative JourneyMetamorphosis: Life's Transformative Journey
Metamorphosis: Life's Transformative Journey
Arshad Shaikh
 
How to manage Multiple Warehouses for multiple floors in odoo point of sale
How to manage Multiple Warehouses for multiple floors in odoo point of saleHow to manage Multiple Warehouses for multiple floors in odoo point of sale
How to manage Multiple Warehouses for multiple floors in odoo point of sale
Celine George
 
Marie Boran Special Collections Librarian Hardiman Library, University of Gal...
Marie Boran Special Collections Librarian Hardiman Library, University of Gal...Marie Boran Special Collections Librarian Hardiman Library, University of Gal...
Marie Boran Special Collections Librarian Hardiman Library, University of Gal...
Library Association of Ireland
 
World war-1(Causes & impacts at a glance) PPT by Simanchala Sarab(BABed,sem-4...
World war-1(Causes & impacts at a glance) PPT by Simanchala Sarab(BABed,sem-4...World war-1(Causes & impacts at a glance) PPT by Simanchala Sarab(BABed,sem-4...
World war-1(Causes & impacts at a glance) PPT by Simanchala Sarab(BABed,sem-4...
larencebapu132
 
Geography Sem II Unit 1C Correlation of Geography with other school subjects
Geography Sem II Unit 1C Correlation of Geography with other school subjectsGeography Sem II Unit 1C Correlation of Geography with other school subjects
Geography Sem II Unit 1C Correlation of Geography with other school subjects
ProfDrShaikhImran
 
How to Set warnings for invoicing specific customers in odoo
How to Set warnings for invoicing specific customers in odooHow to Set warnings for invoicing specific customers in odoo
How to Set warnings for invoicing specific customers in odoo
Celine George
 
SCI BIZ TECH QUIZ (OPEN) PRELIMS XTASY 2025.pptx
SCI BIZ TECH QUIZ (OPEN) PRELIMS XTASY 2025.pptxSCI BIZ TECH QUIZ (OPEN) PRELIMS XTASY 2025.pptx
SCI BIZ TECH QUIZ (OPEN) PRELIMS XTASY 2025.pptx
Ronisha Das
 

php string-part 2

  • 1. Strings TYBSc. Comp. Sci Chapter 2- Functions & Strings monica deshmane(H.V.Desai college) 1
  • 2. Points to learn String • Encoding and escaping • Removing HTML tags • Comparing strings monica deshmane(H.V.Desai college) 2
  • 3. Encoding and Escaping HTML, web page addresses, and database commands are all strings, but they each require different characters to be escaped in different ways. PHP has a number of built-in functions to convert to and from these encodings. these encodings are placed between & and ; “ encoded to &quote; < encoded to &lt; > encoded to &gt; ‘ encoded to &#039; & encoded to &amp; monica deshmane(H.V.Desai college) 3
  • 4. 2 functions 1. Entity-quoting all special characters • htmlentities(string,[quotestyle,character- set]) 2.Entity-quoting only HTML syntax characters • Htmlspecialchars (string,[quotestyle,character-set]) monica deshmane(H.V.Desai college) 4
  • 5. Encoding and Escaping • Entity-quoting all special characters Ex. htmlentities($str, ENT_COMPAT); ENT_COMPAT - convert double-quotes and leave single-quotes as it is. This is bydefault ENT_QUOTES - convert both double and single quotes. ENT_NOQUOTES - leave both double and single quotes as it is. ENT_IGNORE - Silently discard invalid code unit sequences instead of returning an empty string. monica deshmane(H.V.Desai college) 5
  • 6. Entity-quoting only HTML syntax characters • $str = "<html><body>John & 'Jim'</body></html>"; • echo htmlspecialchars($str, ENT_COMPAT); • echo "<br />"; • echo htmlspecialchars($str, ENT_QUOTES); • echo "<br />"; • echo htmlspecialchars($str, ENT_NOQUOTES); • The browser output of the code above will be: • <html><body>John & 'Jim'</body></html> • <html><body>John & 'Jim'</body></html> • <html><body>John & 'Jim'</body></html>monica deshmane(H.V.Desai college) 6
  • 7. Encoding and Escaping $str = "John & 'Jim'"; echo htmlentities($str, ENT_COMPAT); echo "<br />"; echo htmlentities($str, ENT_QUOTES); echo "<br />"; echo htmlentities($str, ENT_NOQUOTES); The browser output of the code above will be: John & 'Jim' John & 'Jim' John & 'Jim' Select "View source" in the browser window: John &amp; 'Jim'<br /> John &amp; &#039; Jim &#039;<br /> John &amp; 'Jim' monica deshmane(H.V.Desai college) 7
  • 8. Entity-quoting only HTML syntax characters • Select "View source" in the browser window: &lt;html&gt;&lt;body&gt;John &amp; 'Jim'&lt;/body&gt;&lt;/html&gt;<br /> &lt;html&gt;&lt;body&gt;John &amp; &#039;Jim&#039;&lt;/body&gt;&lt;/html&gt;<br /> &lt;html&gt;&lt;body&gt;John &amp; 'Jim'&lt;/body&gt;&lt;/html&gt; monica deshmane(H.V.Desai college) 8
  • 9. Entity-quoting only HTML syntax characters • htmlspecialchars(string,quotestyle,character-set) The htmlspecialchars() function converts some • predefined characters to HTML entities. • The predefined characters are: • & (ampersand) becomes &amp; • " (double quote) becomes &quot; • ' (single quote) becomes &#039; • < (less than) becomes &lt; • > (greater than) becomes &gt; monica deshmane(H.V.Desai college) 9
  • 10. Removing HTML tags • Function-: • strip_tags() • What we require? • String • Which tags to allow? • Syntax- • strip_tags(string[, allow]) monica deshmane(H.V.Desai college) 10
  • 11. Removing HTML tags strip_tags() The strip_tags() function strips a string from HTML, XML, and PHP tags. strip_tags(string[, allow]) First parameter is the string to check, allow is Optional that Specifies allowable tags. These tags will not be removed. echo strip_tags("Hello <b><i>world!</i></b>"); Output: Hello world echo strip_tags("Hello <b><i>world!</i></b>", "<b>"); Output: Hello world monica deshmane(H.V.Desai college) 11
  • 12. monica deshmane(H.V.Desai college) 12 add slashes echo addslashes( “it’s interesting” ); Output: “it’s interesting” remove slashes echo stripslashes(“it’s interesting”); Output: “it’s interesting”
  • 13. monica deshmane(H.V.Desai college) 13 –what require to compare 2 strings? Str1 Str2 Syntax- $diff = strcmp($str1, $str2); This function is used for comparing two strings. It returns a number less than 0 if str1 is less than str2, returns greater than 0 if str1 grater than str2 and return 0 if str1 = str2 $str1= “hello"; $str2 = “HELLO"; $diff = strcmp($str1, $str2); echo "$diff"; Output: 1 Comparing Strings 1. strcmp()
  • 14. monica deshmane(H.V.Desai college) 14 Comparing Strings 2. strcasecmp() 2. strcasecmp() – What we require? Syntax- $diff = strcasecmp($str1, $str2); This function converts string to lower case then compares the value. So output will be 0.
  • 15. monica deshmane(H.V.Desai college) 15 What we require? Str1 Str2 No.of letters n to compare Syntax- $comp= strncmp(str1, str2,n); $str1= “hello"; $str2 = “heLLO"; $comp= strcmp($str1, $str2,2); echo "$comp"; //true 4.Strncasecmp //same as strncmp() Comparing Strings 3. Strncmp()
  • 16. monica deshmane(H.V.Desai college) 16 1) soundex() 2) metaphone() Syntax – if (soundex($str) == soundex($str2)){} if (metaphone($str) == metaphone($str2)){} Comparing Strings 5. Approximate equality
  • 17. monica deshmane(H.V.Desai college) 17 1) soundex() - Soundex keys produce the same soundex key according to pronunciation, and can thus be used to simplify searches in databases where you know the pronunciation but not the spelling. $str = “weather";//know, our, son $str2 = “whether"; //no, hour, sun if (soundex($str) == soundex($str2)) { echo "Similar"; } else { echo "Not similar"; } //Similar Comparing Strings 5. Approximate equality
  • 18. monica deshmane(H.V.Desai college) 18 2)metaphone() - Similar to soundex() metaphone creates the same key for similar sounding words. It's more accurate than soundex() as it knows the basic rules of English pronunciation. The metaphone generated keys are of variable length. $str = “weather";//know, our, son $str2 = “whether"; //no, hour, sun if (metaphone($str) == metaphone($str2)) { echo "Similar"; } else { echo "Not similar"; } //Not Similar Comparing Strings 5. Approximate equality
  • 19. monica deshmane(H.V.Desai college) 19 - function returns the number of matching characters of two strings. This function also calculate the similarity of the two strings in percent. What we require? Str1 Str2 Percent of similarity Syntax-: similar_text(str1,str2,percent); Comparing Strings 6. similar_text()
  • 20. monica deshmane(H.V.Desai college) 20 echo "Number of character matching : ".similar_text("Hello World","Hello Welcome", $percent); echo "<br>Percentage : ".$percent; Output: Number of character matching : 8 Percentage : 66.666666666667 Comparing Strings 6. similar_text()
  • 21. monica deshmane(H.V.Desai college) 21 - function returns the Levenshtein distance between two strings. The Levenshtein distance is the number of characters you have to replace, insert or delete to convert string1 into string2. What we require? Str1, Str2 Syntax- $diff= levenshtein(str1,str2); Ex. echo levenshtein("Hello World","ello World"); //1 Comparing Strings 7. levenshtein()
  • 22. monica deshmane(H.V.Desai college) 22 Revise string comparison functions- 1. Strcmp() 2. Strcasecmp() 3. Strncmp() 4. Strncasecmp() 5. Approximate equality 1. soundex() 2. metaphone() 6. similar_text() 7. levenshtein ()
  • 23. End of Presentation monica deshmane(H.V.Desai college) 23