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)

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)

PPTX
PHP string-part 1
monikadeshmane
 
PPTX
php string part 3
monikadeshmane
 
PDF
String functions
Nikul Shah
 
PPT
9780538745840 ppt ch03
Terry Yoast
 
PPT
Manipulating strings
Nicole Ryan
 
PPTX
UNIT II (7).pptx
DrDhivyaaCRAssistant
 
PPTX
UNIT II (7).pptx
DrDhivyaaCRAssistant
 
PPTX
String handling and arrays by Dr.C.R.Dhivyaa Kongu Engineering College
Dhivyaa C.R
 
PDF
php_string.pdf
Sharon Manmothe
 
ODP
PHP Web Programming
Muthuselvam RS
 
PPTX
Tokens in php (php: Hypertext Preprocessor).pptx
BINJAD1
 
PPT
PHP Tutorial (funtion)
Tinnakorn Puttha
 
PPTX
Php string function
Ravi Bhadauria
 
PDF
UNIT4.pdf php basic programming for begginers
AAFREEN SHAIKH
 
PPTX
strings in php how to use different data types in string
vishal choudhary
 
PPTX
PHP Lecture 01 .pptx PHP Lecture 01 pptx
shahgohar1
 
PPTX
String variable in php
chantholnet
 
PPS
PHP Built-in String Validation Functions
Aung Khant
 
PHP string-part 1
monikadeshmane
 
php string part 3
monikadeshmane
 
String functions
Nikul Shah
 
9780538745840 ppt ch03
Terry Yoast
 
Manipulating strings
Nicole Ryan
 
UNIT II (7).pptx
DrDhivyaaCRAssistant
 
UNIT II (7).pptx
DrDhivyaaCRAssistant
 
String handling and arrays by Dr.C.R.Dhivyaa Kongu Engineering College
Dhivyaa C.R
 
php_string.pdf
Sharon Manmothe
 
PHP Web Programming
Muthuselvam RS
 
Tokens in php (php: Hypertext Preprocessor).pptx
BINJAD1
 
PHP Tutorial (funtion)
Tinnakorn Puttha
 
Php string function
Ravi Bhadauria
 
UNIT4.pdf php basic programming for begginers
AAFREEN SHAIKH
 
strings in php how to use different data types in string
vishal choudhary
 
PHP Lecture 01 .pptx PHP Lecture 01 pptx
shahgohar1
 
String variable in php
chantholnet
 
PHP Built-in String Validation Functions
Aung Khant
 
Ad

More from monikadeshmane (19)

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
java script
monikadeshmane
 
PPT
ip1clientserver model
monikadeshmane
 
PPTX
Chap1introppt2php(finally done)
monikadeshmane
 
PPTX
Chap1introppt1php basic
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
 
java script
monikadeshmane
 
ip1clientserver model
monikadeshmane
 
Chap1introppt2php(finally done)
monikadeshmane
 
Chap1introppt1php basic
monikadeshmane
 
Ad

Recently uploaded (20)

PDF
The Power of Compound Interest (Stanford Initiative for Financial Decision-Ma...
Stanford IFDM
 
PDF
Quiz Night Live May 2025 - Intra Pragya Online General Quiz
Pragya - UEM Kolkata Quiz Club
 
PDF
IMPORTANT GUIDELINES FOR M.Sc.ZOOLOGY DISSERTATION
raviralanaresh2
 
PPTX
MATH 8 QUARTER 1 WEEK 1 LESSON 2 PRESENTATION
JohnGuillerNestalBah1
 
PDF
Lean IP - Lecture by Dr Oliver Baldus at the MIPLM 2025
MIPLM
 
PDF
I3PM Case study smart parking 2025 with uptoIP® and ABP
MIPLM
 
PDF
DIGESTION OF CARBOHYDRATES ,PROTEINS AND LIPIDS
raviralanaresh2
 
PDF
TechSoup Microsoft Copilot Nonprofit Use Cases and Live Demo - 2025.06.25.pdf
TechSoup
 
PDF
AI-assisted IP-Design lecture from the MIPLM 2025
MIPLM
 
PPTX
How to Manage Wins & Losses in Odoo 18 CRM
Celine George
 
PDF
Genomics Proteomics and Vaccines 1st Edition Guido Grandi (Editor)
kboqcyuw976
 
PPTX
Life and Career Skills Lesson 2.pptxProtective and Risk Factors of Late Adole...
ryangabrielcatalon40
 
PPTX
How to Setup Automatic Reordering Rule in Odoo 18 Inventory
Celine George
 
PPTX
Building Powerful Agentic AI with Google ADK, MCP, RAG, and Ollama.pptx
Tamanna36
 
DOCX
Lesson 1 - Nature and Inquiry of Research
marvinnbustamante1
 
PDF
Indian National movement PPT by Simanchala Sarab, Covering The INC(Formation,...
Simanchala Sarab, BABed(ITEP Secondary stage) in History student at GNDU Amritsar
 
PDF
Lesson 1 : Science and the Art of Geography Ecosystem
marvinnbustamante1
 
PPTX
How to Configure Taxes in Company Currency in Odoo 18 Accounting
Celine George
 
PPTX
Iván Bornacelly - Presentation of the report - Empowering the workforce in th...
EduSkills OECD
 
PDF
TLE 8 QUARTER 1 MODULE WEEK 1 MATATAG CURRICULUM
denniseraya1997
 
The Power of Compound Interest (Stanford Initiative for Financial Decision-Ma...
Stanford IFDM
 
Quiz Night Live May 2025 - Intra Pragya Online General Quiz
Pragya - UEM Kolkata Quiz Club
 
IMPORTANT GUIDELINES FOR M.Sc.ZOOLOGY DISSERTATION
raviralanaresh2
 
MATH 8 QUARTER 1 WEEK 1 LESSON 2 PRESENTATION
JohnGuillerNestalBah1
 
Lean IP - Lecture by Dr Oliver Baldus at the MIPLM 2025
MIPLM
 
I3PM Case study smart parking 2025 with uptoIP® and ABP
MIPLM
 
DIGESTION OF CARBOHYDRATES ,PROTEINS AND LIPIDS
raviralanaresh2
 
TechSoup Microsoft Copilot Nonprofit Use Cases and Live Demo - 2025.06.25.pdf
TechSoup
 
AI-assisted IP-Design lecture from the MIPLM 2025
MIPLM
 
How to Manage Wins & Losses in Odoo 18 CRM
Celine George
 
Genomics Proteomics and Vaccines 1st Edition Guido Grandi (Editor)
kboqcyuw976
 
Life and Career Skills Lesson 2.pptxProtective and Risk Factors of Late Adole...
ryangabrielcatalon40
 
How to Setup Automatic Reordering Rule in Odoo 18 Inventory
Celine George
 
Building Powerful Agentic AI with Google ADK, MCP, RAG, and Ollama.pptx
Tamanna36
 
Lesson 1 - Nature and Inquiry of Research
marvinnbustamante1
 
Indian National movement PPT by Simanchala Sarab, Covering The INC(Formation,...
Simanchala Sarab, BABed(ITEP Secondary stage) in History student at GNDU Amritsar
 
Lesson 1 : Science and the Art of Geography Ecosystem
marvinnbustamante1
 
How to Configure Taxes in Company Currency in Odoo 18 Accounting
Celine George
 
Iván Bornacelly - Presentation of the report - Empowering the workforce in th...
EduSkills OECD
 
TLE 8 QUARTER 1 MODULE WEEK 1 MATATAG CURRICULUM
denniseraya1997
 

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