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
Ad

Recommended

PPT
Perl Xpath Lightning Talk
ddn123456
 
KEY
Achieving Parsing Sanity In Erlang
Sean Cribbs
 
PDF
PHP for Grown-ups
Manuel Lemos
 
PDF
Slides chapter3part1 ruby-forjavaprogrammers
Giovanni924
 
ZIP
Round PEG, Round Hole - Parsing Functionally
Sean Cribbs
 
KEY
Erlang/OTP for Rubyists
Sean Cribbs
 
PDF
Zend Certification Preparation Tutorial
Lorna Mitchell
 
PDF
Lettering js
davatron5000
 
PPTX
JLIFF: Where we are, and where we're going
Chase Tingley
 
PDF
Ruby quick ref
Tharcius Silva
 
PDF
Ruby cheat sheet
Tharcius Silva
 
PPT
Class 5 - PHP Strings
Ahmed Swilam
 
PPTX
Introduction to PHP Lecture 1
Ajay Khatri
 
PDF
/Regex makes me want to (weep|give up|(╯°□°)╯︵ ┻━┻)\.?/i
brettflorio
 
PPT
Basic perl programming
Thang Nguyen
 
PPT
Schema design short
MongoDB
 
PDF
Working with text, Regular expressions
Krasimir Berov (Красимир Беров)
 
PDF
BabelJS - James Kyle at Modern Web UI
modernweb
 
PPT
Jquery presentation
guest5d87aa6
 
PPTX
Basics of Java Script (JS)
Ajay Khatri
 
PDF
Perl.Hacks.On.Vim
Lin Yo-An
 
PDF
Ruby Topic Maps Tutorial (2007-10-10)
Benjamin Bock
 
PDF
Good Evils In Perl
Kang-min Liu
 
PPT
Dealing with Legacy Perl Code - Peter Scott
O'Reilly Media
 
PDF
Php Crash Course - Macq Electronique 2010
Michelangelo van Dam
 
PPTX
PHP Strings and Patterns
Henry Osborne
 
ODP
Using DAOs without implementing them
benfante
 
PPTX
PHP Powerpoint -- Teach PHP with this
Ian Macali
 
PPSX
Php and MySQL
Tiji Thomas
 
PPT
Csharp4 strings and_regular_expressions
Abed Bukhari
 

More Related Content

What's hot (20)

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

Similar to php string-part 2 (20)

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

More from monikadeshmane (20)

PPTX
File system node js
monikadeshmane
 
PPTX
Nodejs functions & modules
monikadeshmane
 
PPTX
Nodejs buffers
monikadeshmane
 
PPTX
Intsllation & 1st program nodejs
monikadeshmane
 
PPTX
Nodejs basics
monikadeshmane
 
PPTX
Chap 5 php files part-2
monikadeshmane
 
PPTX
Chap 5 php files part 1
monikadeshmane
 
PPTX
Chap4 oop class (php) part 2
monikadeshmane
 
PPTX
Chap4 oop class (php) part 1
monikadeshmane
 
PPTX
Chap 3php array part4
monikadeshmane
 
PPTX
Chap 3php array part 3
monikadeshmane
 
PPTX
Chap 3php array part 2
monikadeshmane
 
PPTX
Chap 3php array part1
monikadeshmane
 
PPTX
PHP function
monikadeshmane
 
PPTX
php string part 4
monikadeshmane
 
PPTX
php string part 3
monikadeshmane
 
PPTX
PHP string-part 1
monikadeshmane
 
PPTX
java script
monikadeshmane
 
PPT
ip1clientserver model
monikadeshmane
 
PPTX
Chap1introppt2php(finally done)
monikadeshmane
 
File system node js
monikadeshmane
 
Nodejs functions & modules
monikadeshmane
 
Nodejs buffers
monikadeshmane
 
Intsllation & 1st program nodejs
monikadeshmane
 
Nodejs basics
monikadeshmane
 
Chap 5 php files part-2
monikadeshmane
 
Chap 5 php files part 1
monikadeshmane
 
Chap4 oop class (php) part 2
monikadeshmane
 
Chap4 oop class (php) part 1
monikadeshmane
 
Chap 3php array part4
monikadeshmane
 
Chap 3php array part 3
monikadeshmane
 
Chap 3php array part 2
monikadeshmane
 
Chap 3php array part1
monikadeshmane
 
PHP function
monikadeshmane
 
php string part 4
monikadeshmane
 
php string part 3
monikadeshmane
 
PHP string-part 1
monikadeshmane
 
java script
monikadeshmane
 
ip1clientserver model
monikadeshmane
 
Chap1introppt2php(finally done)
monikadeshmane
 
Ad

Recently uploaded (20)

PPTX
Photo chemistry Power Point Presentation
mprpgcwa2024
 
PPTX
Pests of Maize: An comprehensive overview.pptx
Arshad Shaikh
 
PPTX
How to Customize Quotation Layouts in Odoo 18
Celine George
 
PPTX
IIT KGP Quiz Week 2024 Sports Quiz (Prelims + Finals)
IIT Kharagpur Quiz Club
 
PPTX
How payment terms are configured in Odoo 18
Celine George
 
PPTX
Values Education 10 Quarter 1 Module .pptx
JBPafin
 
PPTX
Tanja Vujicic - PISA for Schools contact Info
EduSkills OECD
 
PPTX
A Visual Introduction to the Prophet Jeremiah
Steve Thomason
 
PDF
Gladiolous Cultivation practices by AKL.pdf
kushallamichhame
 
PDF
University of Ghana Cracks Down on Misconduct: Over 100 Students Sanctioned
Kweku Zurek
 
PDF
This is why students from these 44 institutions have not received National Se...
Kweku Zurek
 
PDF
Learning Styles Inventory for Senior High School Students
Thelma Villaflores
 
PDF
Hurricane Helene Application Documents Checklists
Mebane Rash
 
PPTX
NSUMD_M1 Library Orientation_June 11, 2025.pptx
Julie Sarpy
 
PDF
LDMMIA Yoga S10 Free Workshop Grad Level
LDM & Mia eStudios
 
PDF
Aprendendo Arquitetura Framework Salesforce - Dia 02
Mauricio Alexandre Silva
 
PDF
Romanticism in Love and Sacrifice An Analysis of Oscar Wilde’s The Nightingal...
KaryanaTantri21
 
PPTX
2025 Completing the Pre-SET Plan Form.pptx
mansk2
 
PPTX
Code Profiling in Odoo 18 - Odoo 18 Slides
Celine George
 
PPTX
GREAT QUIZ EXCHANGE 2025 - GENERAL QUIZ.pptx
Ronisha Das
 
Photo chemistry Power Point Presentation
mprpgcwa2024
 
Pests of Maize: An comprehensive overview.pptx
Arshad Shaikh
 
How to Customize Quotation Layouts in Odoo 18
Celine George
 
IIT KGP Quiz Week 2024 Sports Quiz (Prelims + Finals)
IIT Kharagpur Quiz Club
 
How payment terms are configured in Odoo 18
Celine George
 
Values Education 10 Quarter 1 Module .pptx
JBPafin
 
Tanja Vujicic - PISA for Schools contact Info
EduSkills OECD
 
A Visual Introduction to the Prophet Jeremiah
Steve Thomason
 
Gladiolous Cultivation practices by AKL.pdf
kushallamichhame
 
University of Ghana Cracks Down on Misconduct: Over 100 Students Sanctioned
Kweku Zurek
 
This is why students from these 44 institutions have not received National Se...
Kweku Zurek
 
Learning Styles Inventory for Senior High School Students
Thelma Villaflores
 
Hurricane Helene Application Documents Checklists
Mebane Rash
 
NSUMD_M1 Library Orientation_June 11, 2025.pptx
Julie Sarpy
 
LDMMIA Yoga S10 Free Workshop Grad Level
LDM & Mia eStudios
 
Aprendendo Arquitetura Framework Salesforce - Dia 02
Mauricio Alexandre Silva
 
Romanticism in Love and Sacrifice An Analysis of Oscar Wilde’s The Nightingal...
KaryanaTantri21
 
2025 Completing the Pre-SET Plan Form.pptx
mansk2
 
Code Profiling in Odoo 18 - Odoo 18 Slides
Celine George
 
GREAT QUIZ EXCHANGE 2025 - GENERAL QUIZ.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