SlideShare a Scribd company logo
Strings
TYBSc. Comp. Sci.
Chapter 2- Functions & Strings
monica deshmane(H.V.Desai college) 1
Points to learn
String
• Manipulating strings
• Decomposing string
• String searching functions
• Decomposing URLs
monica deshmane(H.V.Desai college) 2
Manipulating String
• This function searches string or character in a
given string.
• It returns false if string or character is not
found.
• What we require?
• Str1
• Str2
• Syntax-
• $pos = strpos(large string, small string);
monica deshmane(H.V.Desai college) 3
1. strpos()
Manipulating String
$str = "Welcome to PHP Programming, Here is the string";
$pos = strpos($str, “ing");
echo $pos;
pos = 23
strrpos() – This function finds the last occurance of character
in a string.
$pos = strrpos($str, “ing");
echo $pos;
pos = 43
monica deshmane(H.V.Desai college) 4
1. strpos() $pos = strpos(large string, small string);
Manipulating String
monica deshmane(H.V.Desai college) 5
2. substring
Function used To take part of string
What we require?
String
Start index
How much characters to retrieve
Syntax-
substr (string,start,no.of characters)
Manipulating String
monica deshmane(H.V.Desai college) 6
2. substring substr (string,start,no.of characters)
$str1= "Welcome";
$str2 = substr($str1, 3);
$str3 = substr($str1, 2, 3);
echo "$str2 $str3";
Output: come lco
Manipulating String
monica deshmane(H.V.Desai college) 7
3. Substring count
Function used To count how much times small
string present within large string.
What we require?
String(in what)
Small string
How much characters to retrieve
Syntax-
Substr_count (string,substring)
Manipulating String
monica deshmane(H.V.Desai college) 8
3. Substring count Substr_count (string,substring)
$str = <<< my_doc
This is document writtern using heredoc.
This is used to display the multilines.
This is how it works. This ends here.
my_doc;
echo $str;
$cnt = substr_count($str, "is");
echo "<br>Count is : $cnt";
This ends here.
Count is : 8
Manipulating String
monica deshmane(H.V.Desai college) 9
4. Substring replacement
Function used To replace small/ sub string within large string.
What we require?
Large String(in what)
Small string
How much characters to replace
Syntax-
substr_replace($str, substring, start, no.of characters);
Manipulating String
monica deshmane(H.V.Desai college) 10
4. Substring replacement
substr_replace($str, substring, start, no.of characters);
//replace the string
$str = "Welcome to HTML Programming";
$newstr = substr_replace($str, "PHP", 11, 4);
echo $newstr;
Welcome to PHP Programming
//insert without deleting
$newstr = substr_replace($newstr, "Core lang ", 15, 0);
echo "<BR>$newstr";
Welcome to HTML Core lang Programming
//replacement of "" without inserting
$newstr = substr_replace($newstr, "", 15, 5);
echo "<BR>$newstr";
Manipulating String
monica deshmane(H.V.Desai college) 11
4. Substring replacement Continue…
//insert at the beginning of the srting
$newstr = substr_replace($newstr, "Hi ", 0, 0);
echo "<BR>$newstr";
//number of characters from the end of the string from which to start the
replacement
$newstr = substr_replace($newstr, "examples", -11);
echo "<BR>$newstr";
//deletes from start and keeps length characters
$newstr = substr_replace($newstr, "", -12, -8);
echo "<BR>$newstr";
Manipulating String
monica deshmane(H.V.Desai college) 12
5. string replacement
Function used To replace 1 string with other string.
What we require?
Str1(what)
Str2(by what)
Str(in what)
How much characters to replace
Syntax-
Str_replace(what,by what,inwhat,[cnt])
Manipulating String
monica deshmane(H.V.Desai college) 13
5. string replacement Str_replace(what,by what,inwhat,cnt)
//replace the string
$str = "Welcome to HTML Programming";
$newstr = str_replace(“o”,”i”,$str);
echo $newstr;
Manipulating String
monica deshmane(H.V.Desai college) 14
6. String reverse Strrev(string)
//replace the string
$str = "Welcome to HTML Programming";
$newstr = strrev($str);
echo $newstr;
Manipulating String
monica deshmane(H.V.Desai college) 15
7. String repeat Str_repeat(string,count)
echo str_repeat(“*”, 40);
Returns a new string consisting of string repeated number of times.
Manipulating String
monica deshmane(H.V.Desai college) 16
8. String padding
Function used To pad characters in string.
What we require?
string
Total Length
Padding string
Position
Syntax-
str_pad(String, length, padding string,Position);
str_pad() pads one string with another. Bydefault it pads spaces on right hand side.
STR_PAD_LEFT – to pad on left side.
STR_PAD_BOTH – to pad on both the side.
Manipulating String
monica deshmane(H.V.Desai college) 17
8. String padding
str_pad(String, length, padding string,Position);
$str = str_pad("Hello", 10);
echo "<BR>$str Student"; //Hello Student
$str = str_pad("Hello", 10, "*");
echo "<BR>$str Student"; //Hello***** Student
$str = str_pad("Hello", 10, "*", STR_PAD_LEFT);
echo "<BR>$str Student"; //*****Hello Student
$str = str_pad("Hello", 10, "*", STR_PAD_BOTH);
echo "<BR>$str Student“;//**Hello***Student
Revise manipulating string
1. $pos = strpos(large string, small string);
2. $str3 = substr($str1, 2, 3);
3. $cnt = substr_count($str, "This");
4. $newstr = substr_replace($str, "PHP", 11, 4);
5. $newstr = str_replace(“o”,”i”,$str,2);
6. $newstr = strrev($str);
7. echo str_repeat(“*”, 40);
8. $str = str_pad("Hello", 10, "*",
STR_PAD_LEFT);
monica deshmane(H.V.Desai college) 18
monica deshmane(H.V.Desai college) 19
strtok() – First time we need to pass two arguments.
To retrieve next tokens repeatedly call strtok() by passing one argument.
What we require?
String
Seperator
Syntax:-
$token = strtok($str, seperator);
Decomposing string
1. Tokenizing
monica deshmane(H.V.Desai college) 20
$str = "C Programming, Dennis Richie, Wrox publication, 1000";
$token = strtok($str, ",");
while($token != false)
{
echo "$token ";
$token = strtok(",");
}
Output:
C Programming Dennis Richie Wrox publication 1000
Decomposing string
1. Tokenizing $token = strtok($str, seperator);
monica deshmane(H.V.Desai college) 21
sscanf() – This function decomposes a string accoring to a printf(). This function returns
number of fields assigned.
What we require?
String
Scalar factors
Parameters to scan
Decomposing string
2. sscanf
Syntax-
sscanf($str, scalar factors, parameters)
Example-
$n = sscanf($str, "%st%s (%d)", $first, $last, $price);
monica deshmane(H.V.Desai college) 22
sscanf() – This function decomposes a string accoring to a printf().
This function returns number of fields assigned.
$str = "CPPtTechMax (400)";
$n = sscanf($str, "%st%s (%d)", $first, $last, $MRP);
echo "<BR>Math Count : $n";
echo “Count = $n First = $first Second = $last price = $MRP";
Decomposing string
2. sscanf
sscanf($str, scalar factors, parameters)
monica deshmane(H.V.Desai college)
23
explode() - This function constructs array from string.
What we require?
seperator
String
Limit
Syntax-
$array = explode(separator, string[,limit]);
Decomposing string
3. Exploding and imploding-
explode()
$array = explode(separator, string[,limit]);
monica deshmane(H.V.Desai college)
24
$str = "C Programming, Dennis Richie, Wrox publication, 2000";
$arr = explode(",", $str);
print_r($arr);
Output:
Array ( [0] => C Programming [1] => Dennis Richie [2] => Wrox publication [3] => 2000)
•$str = "C Programming, Dennis Richie, Wrox publication, 1000";
$arr = explode(",", $str, 2);
print_r($arr);
Output:
Array ( [0] => C Programming [1] => Dennis Richie, Wrox publication, 1000 )
Decomposing string
3. Exploding and imploding-
explode()
$array = explode(separator, string[,limit]);
monica deshmane(H.V.Desai college)
25
implode() - This function constructs string from array.
This is reverse of explode().
Syntax-
$str = implode(separator, array);
$book = array("C Programming", "Dennis Richie", "Wrox publication", 5000);
$str = implode(",", $book);
echo "<BR>$str";
Decomposing string
3. Exploding and imploding-
implode() $str = implode(separator, array);
String searching functions
monica deshmane(H.V.Desai college) 26
1.strpos()
$pos = strpos(large string, small string);
2. Strstr(string,substring)
1. strpos(seen previously)
2. strstr() or strchr() – This function searches for a given string into
original string. It returns the string starting from given string.
$str = "Welcome to PHP Programming, Here is the string";
$rem = strstr($str, "PHP");
echo "<BR>Remaining str : $rem";
Output: PHP Programming, Here is the string
String searching functions
monica deshmane(H.V.Desai college) 27
3. stristr() 2. Stristr(string,substring)
This function is used to search string. This is case insensitive.
$rem = stristr($str, “php");
echo "<BR>Remaining str : $rem";
Output: PHP Programming, Here is the string
String searching functions
monica deshmane(H.V.Desai college) 28
4.strspn() 5. strcspn()
4.strspn() – This function search for charset in beginning (1st word) of the
specified string. It returns the length depending upon number of characters
appeared from charset.
$length = strspn(string, charset);
$len = strspn("abcdefand",”bac");
Output: 3
$len = strspn(“programming in php",”phpin");
5. strcspn() - This function is reverse of strspn(). It tells you how much of the
start of the string is not composed of characters in character set.
$len = strcspn("abdnefbacnd","nt");
Output: 3
Revise string searching functions
1. $pos = strpos(large string, small string);
2. $rem = strstr($str, "PHP");
3. $rem = stristr($str, “php");
4. $len = strspn("abcdefand",”bac");
5. $len = strcspn("abdnefbacnd","nt");
monica deshmane(H.V.Desai college) 29
Decomposing URLs
monica deshmane(H.V.Desai college) 30
parse_url() parse_url(url)
This function returns array of components of a URL.
The possible keys of the hash are scheme, host, port, user, pass, path, query
and fragment.
$arr = parse_url("http://localhost:8080/onlineexam/test.html");
print_r($arr);
Output:
Array ( [scheme] => http [host] => localhost [port] => 8080 [path] =>
/onlineexam/test.html )
End of Presentation
monica deshmane(H.V.Desai college) 31

More Related Content

What's hot (18)

PDF
Descobrindo a linguagem Perl
garux
 
PDF
Working with text, Regular expressions
Krasimir Berov (Красимир Беров)
 
PDF
Beyond javascript using the features of tomorrow
Alexander Varwijk
 
PPT
PHP - Introduction to String Handling
Vibrant Technologies & Computers
 
PDF
Introdução ao Perl 6
garux
 
PDF
Wx::Perl::Smart
lichtkind
 
PDF
Programming Lisp Clojure - 2장 : 클로저 둘러보기
JangHyuk You
 
PDF
Top 10 pieges php afup limoges
Damien Seguy
 
PDF
Fantastic DSL in Python
kwatch
 
PPT
Unit vii wp ppt
Bhavsingh Maloth
 
PPT
You Can Do It! Start Using Perl to Handle Your Voyager Needs
Roy Zimmer
 
PPTX
Learn python - for beginners - part-2
RajKumar Rampelli
 
PDF
The Error of Our Ways
Kevlin Henney
 
PDF
/Regex makes me want to (weep|give up|(╯°□°)╯︵ ┻━┻)\.?/i
brettflorio
 
PDF
Perl.Hacks.On.Vim
Lin Yo-An
 
PDF
Class 2: Welcome part 2
Marc Gouw
 
Descobrindo a linguagem Perl
garux
 
Working with text, Regular expressions
Krasimir Berov (Красимир Беров)
 
Beyond javascript using the features of tomorrow
Alexander Varwijk
 
PHP - Introduction to String Handling
Vibrant Technologies & Computers
 
Introdução ao Perl 6
garux
 
Wx::Perl::Smart
lichtkind
 
Programming Lisp Clojure - 2장 : 클로저 둘러보기
JangHyuk You
 
Top 10 pieges php afup limoges
Damien Seguy
 
Fantastic DSL in Python
kwatch
 
Unit vii wp ppt
Bhavsingh Maloth
 
You Can Do It! Start Using Perl to Handle Your Voyager Needs
Roy Zimmer
 
Learn python - for beginners - part-2
RajKumar Rampelli
 
The Error of Our Ways
Kevlin Henney
 
/Regex makes me want to (weep|give up|(╯°□°)╯︵ ┻━┻)\.?/i
brettflorio
 
Perl.Hacks.On.Vim
Lin Yo-An
 
Class 2: Welcome part 2
Marc Gouw
 

Similar to php string part 3 (20)

PPTX
PHP string-part 1
monikadeshmane
 
ODP
PHP Web Programming
Muthuselvam RS
 
PDF
php_string.pdf
Sharon Manmothe
 
PPTX
Php functions
JIGAR MAKHIJA
 
PPTX
Regular expressions in Python
Sujith Kumar
 
PPTX
String handling and arrays by Dr.C.R.Dhivyaa Kongu Engineering College
Dhivyaa C.R
 
PPTX
UNIT II (7).pptx
DrDhivyaaCRAssistant
 
PPTX
UNIT II (7).pptx
DrDhivyaaCRAssistant
 
PDF
WordCamp Portland 2018: PHP for WordPress
Alena Holligan
 
PDF
Perl.predefined.variables
King Hom
 
PPTX
Php & my sql
Norhisyam Dasuki
 
PDF
Manipulating string data with a pattern in R
Lun-Hsien Chang
 
PPTX
Tokens in php (php: Hypertext Preprocessor).pptx
BINJAD1
 
PPT
Manipulating strings
Nicole Ryan
 
PPTX
Chap 3php array part 3
monikadeshmane
 
PPSX
What's New In C# 7
Paulo Morgado
 
PPTX
Day5 String python language for btech.pptx
mrsam3062
 
PPTX
Computer programming 2 Lesson 12
MLG College of Learning, Inc
 
PPT
Strings in c
vampugani
 
PDF
Rakudo
awwaiid
 
PHP string-part 1
monikadeshmane
 
PHP Web Programming
Muthuselvam RS
 
php_string.pdf
Sharon Manmothe
 
Php functions
JIGAR MAKHIJA
 
Regular expressions in Python
Sujith Kumar
 
String handling and arrays by Dr.C.R.Dhivyaa Kongu Engineering College
Dhivyaa C.R
 
UNIT II (7).pptx
DrDhivyaaCRAssistant
 
UNIT II (7).pptx
DrDhivyaaCRAssistant
 
WordCamp Portland 2018: PHP for WordPress
Alena Holligan
 
Perl.predefined.variables
King Hom
 
Php & my sql
Norhisyam Dasuki
 
Manipulating string data with a pattern in R
Lun-Hsien Chang
 
Tokens in php (php: Hypertext Preprocessor).pptx
BINJAD1
 
Manipulating strings
Nicole Ryan
 
Chap 3php array part 3
monikadeshmane
 
What's New In C# 7
Paulo Morgado
 
Day5 String python language for btech.pptx
mrsam3062
 
Computer programming 2 Lesson 12
MLG College of Learning, Inc
 
Strings in c
vampugani
 
Rakudo
awwaiid
 
Ad

More from monikadeshmane (17)

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 part1
monikadeshmane
 
PPTX
PHP function
monikadeshmane
 
PPTX
php string-part 2
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 part1
monikadeshmane
 
PHP function
monikadeshmane
 
php string-part 2
monikadeshmane
 
java script
monikadeshmane
 
ip1clientserver model
monikadeshmane
 
Chap1introppt2php(finally done)
monikadeshmane
 
Chap1introppt1php basic
monikadeshmane
 
Ad

Recently uploaded (20)

PDF
Free eBook ~100 Common English Proverbs (ebook) pdf.pdf
OH TEIK BIN
 
PPTX
Ward Management: Patient Care, Personnel, Equipment, and Environment.pptx
PRADEEP ABOTHU
 
PPTX
How to Setup Automatic Reordering Rule in Odoo 18 Inventory
Celine George
 
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
DIGESTION OF CARBOHYDRATES ,PROTEINS AND LIPIDS
raviralanaresh2
 
PDF
CAD25 Gbadago and Fafa Presentation Revised-Aston Business School, UK.pdf
Kweku Zurek
 
PDF
WATERSHED MANAGEMENT CASE STUDIES - ULUGURU MOUNTAINS AND ARVARI RIVERpdf
Ar.Asna
 
PDF
Our Guide to the July 2025 USPS® Rate Change
Postal Advocate Inc.
 
PPTX
How to Configure Taxes in Company Currency in Odoo 18 Accounting
Celine George
 
PPTX
grade 8 week 2 ict.pptx. matatag grade 7
VanessaTaberlo
 
PDF
Supply Chain Security A Comprehensive Approach 1st Edition Arthur G. Arway
rxgnika452
 
PDF
Genomics Proteomics and Vaccines 1st Edition Guido Grandi (Editor)
kboqcyuw976
 
PDF
Wikinomics How Mass Collaboration Changes Everything Don Tapscott
wcsqyzf5909
 
PPTX
Aerobic and Anaerobic respiration and CPR.pptx
Olivier Rochester
 
PDF
TechSoup Microsoft Copilot Nonprofit Use Cases and Live Demo - 2025.06.25.pdf
TechSoup
 
PDF
Andreas Schleicher_Teaching Compass_Education 2040.pdf
EduSkills OECD
 
PPTX
How to Configure Refusal of Applicants in Odoo 18 Recruitment
Celine George
 
PPTX
Elo the Hero is an story about a young boy who became hero.
TeacherEmily1
 
PDF
I3PM Industry Case Study Siemens on Strategic and Value-Oriented IP Management
MIPLM
 
PDF
Quiz Night Live May 2025 - Intra Pragya Online General Quiz
Pragya - UEM Kolkata Quiz Club
 
Free eBook ~100 Common English Proverbs (ebook) pdf.pdf
OH TEIK BIN
 
Ward Management: Patient Care, Personnel, Equipment, and Environment.pptx
PRADEEP ABOTHU
 
How to Setup Automatic Reordering Rule in Odoo 18 Inventory
Celine George
 
Indian National movement PPT by Simanchala Sarab, Covering The INC(Formation,...
Simanchala Sarab, BABed(ITEP Secondary stage) in History student at GNDU Amritsar
 
DIGESTION OF CARBOHYDRATES ,PROTEINS AND LIPIDS
raviralanaresh2
 
CAD25 Gbadago and Fafa Presentation Revised-Aston Business School, UK.pdf
Kweku Zurek
 
WATERSHED MANAGEMENT CASE STUDIES - ULUGURU MOUNTAINS AND ARVARI RIVERpdf
Ar.Asna
 
Our Guide to the July 2025 USPS® Rate Change
Postal Advocate Inc.
 
How to Configure Taxes in Company Currency in Odoo 18 Accounting
Celine George
 
grade 8 week 2 ict.pptx. matatag grade 7
VanessaTaberlo
 
Supply Chain Security A Comprehensive Approach 1st Edition Arthur G. Arway
rxgnika452
 
Genomics Proteomics and Vaccines 1st Edition Guido Grandi (Editor)
kboqcyuw976
 
Wikinomics How Mass Collaboration Changes Everything Don Tapscott
wcsqyzf5909
 
Aerobic and Anaerobic respiration and CPR.pptx
Olivier Rochester
 
TechSoup Microsoft Copilot Nonprofit Use Cases and Live Demo - 2025.06.25.pdf
TechSoup
 
Andreas Schleicher_Teaching Compass_Education 2040.pdf
EduSkills OECD
 
How to Configure Refusal of Applicants in Odoo 18 Recruitment
Celine George
 
Elo the Hero is an story about a young boy who became hero.
TeacherEmily1
 
I3PM Industry Case Study Siemens on Strategic and Value-Oriented IP Management
MIPLM
 
Quiz Night Live May 2025 - Intra Pragya Online General Quiz
Pragya - UEM Kolkata Quiz Club
 

php string part 3

  • 1. Strings TYBSc. Comp. Sci. Chapter 2- Functions & Strings monica deshmane(H.V.Desai college) 1
  • 2. Points to learn String • Manipulating strings • Decomposing string • String searching functions • Decomposing URLs monica deshmane(H.V.Desai college) 2
  • 3. Manipulating String • This function searches string or character in a given string. • It returns false if string or character is not found. • What we require? • Str1 • Str2 • Syntax- • $pos = strpos(large string, small string); monica deshmane(H.V.Desai college) 3 1. strpos()
  • 4. Manipulating String $str = "Welcome to PHP Programming, Here is the string"; $pos = strpos($str, “ing"); echo $pos; pos = 23 strrpos() – This function finds the last occurance of character in a string. $pos = strrpos($str, “ing"); echo $pos; pos = 43 monica deshmane(H.V.Desai college) 4 1. strpos() $pos = strpos(large string, small string);
  • 5. Manipulating String monica deshmane(H.V.Desai college) 5 2. substring Function used To take part of string What we require? String Start index How much characters to retrieve Syntax- substr (string,start,no.of characters)
  • 6. Manipulating String monica deshmane(H.V.Desai college) 6 2. substring substr (string,start,no.of characters) $str1= "Welcome"; $str2 = substr($str1, 3); $str3 = substr($str1, 2, 3); echo "$str2 $str3"; Output: come lco
  • 7. Manipulating String monica deshmane(H.V.Desai college) 7 3. Substring count Function used To count how much times small string present within large string. What we require? String(in what) Small string How much characters to retrieve Syntax- Substr_count (string,substring)
  • 8. Manipulating String monica deshmane(H.V.Desai college) 8 3. Substring count Substr_count (string,substring) $str = <<< my_doc This is document writtern using heredoc. This is used to display the multilines. This is how it works. This ends here. my_doc; echo $str; $cnt = substr_count($str, "is"); echo "<br>Count is : $cnt"; This ends here. Count is : 8
  • 9. Manipulating String monica deshmane(H.V.Desai college) 9 4. Substring replacement Function used To replace small/ sub string within large string. What we require? Large String(in what) Small string How much characters to replace Syntax- substr_replace($str, substring, start, no.of characters);
  • 10. Manipulating String monica deshmane(H.V.Desai college) 10 4. Substring replacement substr_replace($str, substring, start, no.of characters); //replace the string $str = "Welcome to HTML Programming"; $newstr = substr_replace($str, "PHP", 11, 4); echo $newstr; Welcome to PHP Programming //insert without deleting $newstr = substr_replace($newstr, "Core lang ", 15, 0); echo "<BR>$newstr"; Welcome to HTML Core lang Programming //replacement of "" without inserting $newstr = substr_replace($newstr, "", 15, 5); echo "<BR>$newstr";
  • 11. Manipulating String monica deshmane(H.V.Desai college) 11 4. Substring replacement Continue… //insert at the beginning of the srting $newstr = substr_replace($newstr, "Hi ", 0, 0); echo "<BR>$newstr"; //number of characters from the end of the string from which to start the replacement $newstr = substr_replace($newstr, "examples", -11); echo "<BR>$newstr"; //deletes from start and keeps length characters $newstr = substr_replace($newstr, "", -12, -8); echo "<BR>$newstr";
  • 12. Manipulating String monica deshmane(H.V.Desai college) 12 5. string replacement Function used To replace 1 string with other string. What we require? Str1(what) Str2(by what) Str(in what) How much characters to replace Syntax- Str_replace(what,by what,inwhat,[cnt])
  • 13. Manipulating String monica deshmane(H.V.Desai college) 13 5. string replacement Str_replace(what,by what,inwhat,cnt) //replace the string $str = "Welcome to HTML Programming"; $newstr = str_replace(“o”,”i”,$str); echo $newstr;
  • 14. Manipulating String monica deshmane(H.V.Desai college) 14 6. String reverse Strrev(string) //replace the string $str = "Welcome to HTML Programming"; $newstr = strrev($str); echo $newstr;
  • 15. Manipulating String monica deshmane(H.V.Desai college) 15 7. String repeat Str_repeat(string,count) echo str_repeat(“*”, 40); Returns a new string consisting of string repeated number of times.
  • 16. Manipulating String monica deshmane(H.V.Desai college) 16 8. String padding Function used To pad characters in string. What we require? string Total Length Padding string Position Syntax- str_pad(String, length, padding string,Position); str_pad() pads one string with another. Bydefault it pads spaces on right hand side. STR_PAD_LEFT – to pad on left side. STR_PAD_BOTH – to pad on both the side.
  • 17. Manipulating String monica deshmane(H.V.Desai college) 17 8. String padding str_pad(String, length, padding string,Position); $str = str_pad("Hello", 10); echo "<BR>$str Student"; //Hello Student $str = str_pad("Hello", 10, "*"); echo "<BR>$str Student"; //Hello***** Student $str = str_pad("Hello", 10, "*", STR_PAD_LEFT); echo "<BR>$str Student"; //*****Hello Student $str = str_pad("Hello", 10, "*", STR_PAD_BOTH); echo "<BR>$str Student“;//**Hello***Student
  • 18. Revise manipulating string 1. $pos = strpos(large string, small string); 2. $str3 = substr($str1, 2, 3); 3. $cnt = substr_count($str, "This"); 4. $newstr = substr_replace($str, "PHP", 11, 4); 5. $newstr = str_replace(“o”,”i”,$str,2); 6. $newstr = strrev($str); 7. echo str_repeat(“*”, 40); 8. $str = str_pad("Hello", 10, "*", STR_PAD_LEFT); monica deshmane(H.V.Desai college) 18
  • 19. monica deshmane(H.V.Desai college) 19 strtok() – First time we need to pass two arguments. To retrieve next tokens repeatedly call strtok() by passing one argument. What we require? String Seperator Syntax:- $token = strtok($str, seperator); Decomposing string 1. Tokenizing
  • 20. monica deshmane(H.V.Desai college) 20 $str = "C Programming, Dennis Richie, Wrox publication, 1000"; $token = strtok($str, ","); while($token != false) { echo "$token "; $token = strtok(","); } Output: C Programming Dennis Richie Wrox publication 1000 Decomposing string 1. Tokenizing $token = strtok($str, seperator);
  • 21. monica deshmane(H.V.Desai college) 21 sscanf() – This function decomposes a string accoring to a printf(). This function returns number of fields assigned. What we require? String Scalar factors Parameters to scan Decomposing string 2. sscanf Syntax- sscanf($str, scalar factors, parameters) Example- $n = sscanf($str, "%st%s (%d)", $first, $last, $price);
  • 22. monica deshmane(H.V.Desai college) 22 sscanf() – This function decomposes a string accoring to a printf(). This function returns number of fields assigned. $str = "CPPtTechMax (400)"; $n = sscanf($str, "%st%s (%d)", $first, $last, $MRP); echo "<BR>Math Count : $n"; echo “Count = $n First = $first Second = $last price = $MRP"; Decomposing string 2. sscanf sscanf($str, scalar factors, parameters)
  • 23. monica deshmane(H.V.Desai college) 23 explode() - This function constructs array from string. What we require? seperator String Limit Syntax- $array = explode(separator, string[,limit]); Decomposing string 3. Exploding and imploding- explode() $array = explode(separator, string[,limit]);
  • 24. monica deshmane(H.V.Desai college) 24 $str = "C Programming, Dennis Richie, Wrox publication, 2000"; $arr = explode(",", $str); print_r($arr); Output: Array ( [0] => C Programming [1] => Dennis Richie [2] => Wrox publication [3] => 2000) •$str = "C Programming, Dennis Richie, Wrox publication, 1000"; $arr = explode(",", $str, 2); print_r($arr); Output: Array ( [0] => C Programming [1] => Dennis Richie, Wrox publication, 1000 ) Decomposing string 3. Exploding and imploding- explode() $array = explode(separator, string[,limit]);
  • 25. monica deshmane(H.V.Desai college) 25 implode() - This function constructs string from array. This is reverse of explode(). Syntax- $str = implode(separator, array); $book = array("C Programming", "Dennis Richie", "Wrox publication", 5000); $str = implode(",", $book); echo "<BR>$str"; Decomposing string 3. Exploding and imploding- implode() $str = implode(separator, array);
  • 26. String searching functions monica deshmane(H.V.Desai college) 26 1.strpos() $pos = strpos(large string, small string); 2. Strstr(string,substring) 1. strpos(seen previously) 2. strstr() or strchr() – This function searches for a given string into original string. It returns the string starting from given string. $str = "Welcome to PHP Programming, Here is the string"; $rem = strstr($str, "PHP"); echo "<BR>Remaining str : $rem"; Output: PHP Programming, Here is the string
  • 27. String searching functions monica deshmane(H.V.Desai college) 27 3. stristr() 2. Stristr(string,substring) This function is used to search string. This is case insensitive. $rem = stristr($str, “php"); echo "<BR>Remaining str : $rem"; Output: PHP Programming, Here is the string
  • 28. String searching functions monica deshmane(H.V.Desai college) 28 4.strspn() 5. strcspn() 4.strspn() – This function search for charset in beginning (1st word) of the specified string. It returns the length depending upon number of characters appeared from charset. $length = strspn(string, charset); $len = strspn("abcdefand",”bac"); Output: 3 $len = strspn(“programming in php",”phpin"); 5. strcspn() - This function is reverse of strspn(). It tells you how much of the start of the string is not composed of characters in character set. $len = strcspn("abdnefbacnd","nt"); Output: 3
  • 29. Revise string searching functions 1. $pos = strpos(large string, small string); 2. $rem = strstr($str, "PHP"); 3. $rem = stristr($str, “php"); 4. $len = strspn("abcdefand",”bac"); 5. $len = strcspn("abdnefbacnd","nt"); monica deshmane(H.V.Desai college) 29
  • 30. Decomposing URLs monica deshmane(H.V.Desai college) 30 parse_url() parse_url(url) This function returns array of components of a URL. The possible keys of the hash are scheme, host, port, user, pass, path, query and fragment. $arr = parse_url("http://localhost:8080/onlineexam/test.html"); print_r($arr); Output: Array ( [scheme] => http [host] => localhost [port] => 8080 [path] => /onlineexam/test.html )
  • 31. End of Presentation monica deshmane(H.V.Desai college) 31