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)

Descobrindo a linguagem Perl
Descobrindo a linguagem PerlDescobrindo a linguagem Perl
Descobrindo a linguagem Perl
garux
 
Working with text, Regular expressions
Working with text, Regular expressionsWorking with text, Regular expressions
Working with text, Regular expressions
Krasimir Berov (Красимир Беров)
 
Hashes
HashesHashes
Hashes
Krasimir Berov (Красимир Беров)
 
Beyond javascript using the features of tomorrow
Beyond javascript   using the features of tomorrowBeyond javascript   using the features of tomorrow
Beyond javascript using the features of tomorrow
Alexander Varwijk
 
PHP - Introduction to String Handling
PHP -  Introduction to  String Handling PHP -  Introduction to  String Handling
PHP - Introduction to String Handling
Vibrant Technologies & Computers
 
Introdução ao Perl 6
Introdução ao Perl 6Introdução ao Perl 6
Introdução ao Perl 6
garux
 
Wx::Perl::Smart
Wx::Perl::SmartWx::Perl::Smart
Wx::Perl::Smart
lichtkind
 
Programming Lisp Clojure - 2장 : 클로저 둘러보기
Programming Lisp Clojure - 2장 : 클로저 둘러보기Programming Lisp Clojure - 2장 : 클로저 둘러보기
Programming Lisp Clojure - 2장 : 클로저 둘러보기
JangHyuk You
 
Top 10 pieges php afup limoges
Top 10 pieges php   afup limogesTop 10 pieges php   afup limoges
Top 10 pieges php afup limoges
Damien Seguy
 
Fantastic DSL in Python
Fantastic DSL in PythonFantastic DSL in Python
Fantastic DSL in Python
kwatch
 
Unit vii wp ppt
Unit vii wp pptUnit vii wp ppt
Unit vii wp ppt
Bhavsingh Maloth
 
You Can Do It! Start Using Perl to Handle Your Voyager Needs
You Can Do It! Start Using Perl to Handle Your Voyager NeedsYou Can Do It! Start Using Perl to Handle Your Voyager Needs
You Can Do It! Start Using Perl to Handle Your Voyager Needs
Roy Zimmer
 
Learn python - for beginners - part-2
Learn python - for beginners - part-2Learn python - for beginners - part-2
Learn python - for beginners - part-2
RajKumar Rampelli
 
The Error of Our Ways
The Error of Our WaysThe Error of Our Ways
The Error of Our Ways
Kevlin Henney
 
Lists and arrays
Lists and arraysLists and arrays
Lists and arrays
Krasimir Berov (Красимир Беров)
 
/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
 
Perl.Hacks.On.Vim
Perl.Hacks.On.VimPerl.Hacks.On.Vim
Perl.Hacks.On.Vim
Lin Yo-An
 
Class 2: Welcome part 2
Class 2: Welcome part 2Class 2: Welcome part 2
Class 2: Welcome part 2
Marc Gouw
 
Descobrindo a linguagem Perl
Descobrindo a linguagem PerlDescobrindo a linguagem Perl
Descobrindo a linguagem Perl
garux
 
Beyond javascript using the features of tomorrow
Beyond javascript   using the features of tomorrowBeyond javascript   using the features of tomorrow
Beyond javascript using the features of tomorrow
Alexander Varwijk
 
Introdução ao Perl 6
Introdução ao Perl 6Introdução ao Perl 6
Introdução ao Perl 6
garux
 
Wx::Perl::Smart
Wx::Perl::SmartWx::Perl::Smart
Wx::Perl::Smart
lichtkind
 
Programming Lisp Clojure - 2장 : 클로저 둘러보기
Programming Lisp Clojure - 2장 : 클로저 둘러보기Programming Lisp Clojure - 2장 : 클로저 둘러보기
Programming Lisp Clojure - 2장 : 클로저 둘러보기
JangHyuk You
 
Top 10 pieges php afup limoges
Top 10 pieges php   afup limogesTop 10 pieges php   afup limoges
Top 10 pieges php afup limoges
Damien Seguy
 
Fantastic DSL in Python
Fantastic DSL in PythonFantastic DSL in Python
Fantastic DSL in Python
kwatch
 
You Can Do It! Start Using Perl to Handle Your Voyager Needs
You Can Do It! Start Using Perl to Handle Your Voyager NeedsYou Can Do It! Start Using Perl to Handle Your Voyager Needs
You Can Do It! Start Using Perl to Handle Your Voyager Needs
Roy Zimmer
 
Learn python - for beginners - part-2
Learn python - for beginners - part-2Learn python - for beginners - part-2
Learn python - for beginners - part-2
RajKumar Rampelli
 
The Error of Our Ways
The Error of Our WaysThe Error of Our Ways
The Error of Our Ways
Kevlin Henney
 
/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
 
Perl.Hacks.On.Vim
Perl.Hacks.On.VimPerl.Hacks.On.Vim
Perl.Hacks.On.Vim
Lin Yo-An
 
Class 2: Welcome part 2
Class 2: Welcome part 2Class 2: Welcome part 2
Class 2: Welcome part 2
Marc Gouw
 

Similar to php string part 3 (20)

PHP string-part 1
PHP string-part 1PHP string-part 1
PHP string-part 1
monikadeshmane
 
String functions
String functionsString functions
String functions
Nikul Shah
 
UNIT II (7).pptx
UNIT II (7).pptxUNIT II (7).pptx
UNIT II (7).pptx
DrDhivyaaCRAssistant
 
UNIT II (7).pptx
UNIT II (7).pptxUNIT II (7).pptx
UNIT II (7).pptx
DrDhivyaaCRAssistant
 
String handling and arrays by Dr.C.R.Dhivyaa Kongu Engineering College
String handling and arrays by Dr.C.R.Dhivyaa Kongu Engineering CollegeString handling and arrays by Dr.C.R.Dhivyaa Kongu Engineering College
String handling and arrays by Dr.C.R.Dhivyaa Kongu Engineering College
Dhivyaa C.R
 
Tokens in php (php: Hypertext Preprocessor).pptx
Tokens in  php (php: Hypertext Preprocessor).pptxTokens in  php (php: Hypertext Preprocessor).pptx
Tokens in php (php: Hypertext Preprocessor).pptx
BINJAD1
 
strings in php how to use different data types in string
strings in php how to use different data types in stringstrings in php how to use different data types in string
strings in php how to use different data types in string
vishal choudhary
 
PHP Web Programming
PHP Web ProgrammingPHP Web Programming
PHP Web Programming
Muthuselvam RS
 
Strings cprogramminglanguagedsasheet.pptx
Strings cprogramminglanguagedsasheet.pptxStrings cprogramminglanguagedsasheet.pptx
Strings cprogramminglanguagedsasheet.pptx
hyundaitvhamari
 
Php string function
Php string function Php string function
Php string function
Ravi Bhadauria
 
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
 
Php, mysqlpart2
Php, mysqlpart2Php, mysqlpart2
Php, mysqlpart2
Subhasis Nayak
 
Chapter 11 Strings.pptx
Chapter 11 Strings.pptxChapter 11 Strings.pptx
Chapter 11 Strings.pptx
afsheenfaiq2
 
php_string.pdf
php_string.pdfphp_string.pdf
php_string.pdf
Sharon Manmothe
 
Php functions
Php functionsPhp functions
Php functions
JIGAR MAKHIJA
 
Array String - Web Programming
Array String - Web ProgrammingArray String - Web Programming
Array String - Web Programming
Amirul Azhar
 
String functions and operations
String functions and operations String functions and operations
String functions and operations
Mudasir Syed
 
php string-part 2
php string-part 2php string-part 2
php string-part 2
monikadeshmane
 
9780538745840 ppt ch03
9780538745840 ppt ch039780538745840 ppt ch03
9780538745840 ppt ch03
Terry Yoast
 
Chapter 14 strings
Chapter 14 stringsChapter 14 strings
Chapter 14 strings
Praveen M Jigajinni
 
String functions
String functionsString functions
String functions
Nikul Shah
 
String handling and arrays by Dr.C.R.Dhivyaa Kongu Engineering College
String handling and arrays by Dr.C.R.Dhivyaa Kongu Engineering CollegeString handling and arrays by Dr.C.R.Dhivyaa Kongu Engineering College
String handling and arrays by Dr.C.R.Dhivyaa Kongu Engineering College
Dhivyaa C.R
 
Tokens in php (php: Hypertext Preprocessor).pptx
Tokens in  php (php: Hypertext Preprocessor).pptxTokens in  php (php: Hypertext Preprocessor).pptx
Tokens in php (php: Hypertext Preprocessor).pptx
BINJAD1
 
strings in php how to use different data types in string
strings in php how to use different data types in stringstrings in php how to use different data types in string
strings in php how to use different data types in string
vishal choudhary
 
Strings cprogramminglanguagedsasheet.pptx
Strings cprogramminglanguagedsasheet.pptxStrings cprogramminglanguagedsasheet.pptx
Strings cprogramminglanguagedsasheet.pptx
hyundaitvhamari
 
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
 
Chapter 11 Strings.pptx
Chapter 11 Strings.pptxChapter 11 Strings.pptx
Chapter 11 Strings.pptx
afsheenfaiq2
 
Array String - Web Programming
Array String - Web ProgrammingArray String - Web Programming
Array String - Web Programming
Amirul Azhar
 
String functions and operations
String functions and operations String functions and operations
String functions and operations
Mudasir Syed
 
9780538745840 ppt ch03
9780538745840 ppt ch039780538745840 ppt ch03
9780538745840 ppt ch03
Terry Yoast
 
Ad

More from monikadeshmane (17)

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 part1
Chap 3php array part1Chap 3php array part1
Chap 3php array part1
monikadeshmane
 
PHP function
PHP functionPHP function
PHP function
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
 
Chap1introppt1php basic
Chap1introppt1php basicChap1introppt1php basic
Chap1introppt1php basic
monikadeshmane
 
Ad

Recently uploaded (20)

Overview of Employee in Odoo 18 - Odoo Slides
Overview of Employee in Odoo 18 - Odoo SlidesOverview of Employee in Odoo 18 - Odoo Slides
Overview of Employee in Odoo 18 - Odoo Slides
Celine George
 
THERAPEUTIC COMMUNICATION included definition, characteristics, nurse patient...
THERAPEUTIC COMMUNICATION included definition, characteristics, nurse patient...THERAPEUTIC COMMUNICATION included definition, characteristics, nurse patient...
THERAPEUTIC COMMUNICATION included definition, characteristics, nurse patient...
parmarjuli1412
 
Overview of Off Boarding in Odoo 18 Employees
Overview of Off Boarding in Odoo 18 EmployeesOverview of Off Boarding in Odoo 18 Employees
Overview of Off Boarding in Odoo 18 Employees
Celine George
 
Paper 109 | Archetypal Journeys in ‘Interstellar’: Exploring Universal Themes...
Paper 109 | Archetypal Journeys in ‘Interstellar’: Exploring Universal Themes...Paper 109 | Archetypal Journeys in ‘Interstellar’: Exploring Universal Themes...
Paper 109 | Archetypal Journeys in ‘Interstellar’: Exploring Universal Themes...
Rajdeep Bavaliya
 
What is FIle and explanation of text files.pptx
What is FIle and explanation of text files.pptxWhat is FIle and explanation of text files.pptx
What is FIle and explanation of text files.pptx
Ramakrishna Reddy Bijjam
 
The Man In The Back – Exceptional Delaware.pdf
The Man In The Back – Exceptional Delaware.pdfThe Man In The Back – Exceptional Delaware.pdf
The Man In The Back – Exceptional Delaware.pdf
dennisongomezk
 
IDF 30min presentation - December 2, 2024.pptx
IDF 30min presentation - December 2, 2024.pptxIDF 30min presentation - December 2, 2024.pptx
IDF 30min presentation - December 2, 2024.pptx
ArneeAgligar
 
How to Create an Event in Odoo 18 - Odoo 18 Slides
How to Create an Event in Odoo 18 - Odoo 18 SlidesHow to Create an Event in Odoo 18 - Odoo 18 Slides
How to Create an Event in Odoo 18 - Odoo 18 Slides
Celine George
 
BINARY files CSV files JSON files with example.pptx
BINARY files CSV files JSON files with example.pptxBINARY files CSV files JSON files with example.pptx
BINARY files CSV files JSON files with example.pptx
Ramakrishna Reddy Bijjam
 
Basic English for Communication - Dr Hj Euis Eti Rohaeti Mpd
Basic English for Communication - Dr Hj Euis Eti Rohaeti MpdBasic English for Communication - Dr Hj Euis Eti Rohaeti Mpd
Basic English for Communication - Dr Hj Euis Eti Rohaeti Mpd
Restu Bias Primandhika
 
How to Configure Vendor Management in Lunch App of Odoo 18
How to Configure Vendor Management in Lunch App of Odoo 18How to Configure Vendor Management in Lunch App of Odoo 18
How to Configure Vendor Management in Lunch App of Odoo 18
Celine George
 
How to Manage Upselling of Subscriptions in Odoo 18
How to Manage Upselling of Subscriptions in Odoo 18How to Manage Upselling of Subscriptions in Odoo 18
How to Manage Upselling of Subscriptions in Odoo 18
Celine George
 
Paper 108 | Thoreau’s Influence on Gandhi: The Evolution of Civil Disobedience
Paper 108 | Thoreau’s Influence on Gandhi: The Evolution of Civil DisobediencePaper 108 | Thoreau’s Influence on Gandhi: The Evolution of Civil Disobedience
Paper 108 | Thoreau’s Influence on Gandhi: The Evolution of Civil Disobedience
Rajdeep Bavaliya
 
Webcrawler_Mule_AIChain_MuleSoft_Meetup_Hyderabad
Webcrawler_Mule_AIChain_MuleSoft_Meetup_HyderabadWebcrawler_Mule_AIChain_MuleSoft_Meetup_Hyderabad
Webcrawler_Mule_AIChain_MuleSoft_Meetup_Hyderabad
Veera Pallapu
 
Unit 3 Poster Sketches with annotations.pptx
Unit 3 Poster Sketches with annotations.pptxUnit 3 Poster Sketches with annotations.pptx
Unit 3 Poster Sketches with annotations.pptx
bobby205207
 
ROLE PLAY: FIRST AID -CPR & RECOVERY POSITION.pptx
ROLE PLAY: FIRST AID -CPR & RECOVERY POSITION.pptxROLE PLAY: FIRST AID -CPR & RECOVERY POSITION.pptx
ROLE PLAY: FIRST AID -CPR & RECOVERY POSITION.pptx
Belicia R.S
 
ABCs of Bookkeeping for Nonprofits TechSoup.pdf
ABCs of Bookkeeping for Nonprofits TechSoup.pdfABCs of Bookkeeping for Nonprofits TechSoup.pdf
ABCs of Bookkeeping for Nonprofits TechSoup.pdf
TechSoup
 
MATERI PPT TOPIK 1 LANDASAN FILOSOFIS PENDIDIKAN
MATERI PPT TOPIK 1 LANDASAN FILOSOFIS PENDIDIKANMATERI PPT TOPIK 1 LANDASAN FILOSOFIS PENDIDIKAN
MATERI PPT TOPIK 1 LANDASAN FILOSOFIS PENDIDIKAN
aditya23173
 
Energy Balances Of Oecd Countries 2011 Iea Statistics 1st Edition Oecd
Energy Balances Of Oecd Countries 2011 Iea Statistics 1st Edition OecdEnergy Balances Of Oecd Countries 2011 Iea Statistics 1st Edition Oecd
Energy Balances Of Oecd Countries 2011 Iea Statistics 1st Edition Oecd
razelitouali
 
MATERI PPT TOPIK 4 LANDASAN FILOSOFIS PENDIDIKAN
MATERI PPT TOPIK 4 LANDASAN FILOSOFIS PENDIDIKANMATERI PPT TOPIK 4 LANDASAN FILOSOFIS PENDIDIKAN
MATERI PPT TOPIK 4 LANDASAN FILOSOFIS PENDIDIKAN
aditya23173
 
Overview of Employee in Odoo 18 - Odoo Slides
Overview of Employee in Odoo 18 - Odoo SlidesOverview of Employee in Odoo 18 - Odoo Slides
Overview of Employee in Odoo 18 - Odoo Slides
Celine George
 
THERAPEUTIC COMMUNICATION included definition, characteristics, nurse patient...
THERAPEUTIC COMMUNICATION included definition, characteristics, nurse patient...THERAPEUTIC COMMUNICATION included definition, characteristics, nurse patient...
THERAPEUTIC COMMUNICATION included definition, characteristics, nurse patient...
parmarjuli1412
 
Overview of Off Boarding in Odoo 18 Employees
Overview of Off Boarding in Odoo 18 EmployeesOverview of Off Boarding in Odoo 18 Employees
Overview of Off Boarding in Odoo 18 Employees
Celine George
 
Paper 109 | Archetypal Journeys in ‘Interstellar’: Exploring Universal Themes...
Paper 109 | Archetypal Journeys in ‘Interstellar’: Exploring Universal Themes...Paper 109 | Archetypal Journeys in ‘Interstellar’: Exploring Universal Themes...
Paper 109 | Archetypal Journeys in ‘Interstellar’: Exploring Universal Themes...
Rajdeep Bavaliya
 
What is FIle and explanation of text files.pptx
What is FIle and explanation of text files.pptxWhat is FIle and explanation of text files.pptx
What is FIle and explanation of text files.pptx
Ramakrishna Reddy Bijjam
 
The Man In The Back – Exceptional Delaware.pdf
The Man In The Back – Exceptional Delaware.pdfThe Man In The Back – Exceptional Delaware.pdf
The Man In The Back – Exceptional Delaware.pdf
dennisongomezk
 
IDF 30min presentation - December 2, 2024.pptx
IDF 30min presentation - December 2, 2024.pptxIDF 30min presentation - December 2, 2024.pptx
IDF 30min presentation - December 2, 2024.pptx
ArneeAgligar
 
How to Create an Event in Odoo 18 - Odoo 18 Slides
How to Create an Event in Odoo 18 - Odoo 18 SlidesHow to Create an Event in Odoo 18 - Odoo 18 Slides
How to Create an Event in Odoo 18 - Odoo 18 Slides
Celine George
 
BINARY files CSV files JSON files with example.pptx
BINARY files CSV files JSON files with example.pptxBINARY files CSV files JSON files with example.pptx
BINARY files CSV files JSON files with example.pptx
Ramakrishna Reddy Bijjam
 
Basic English for Communication - Dr Hj Euis Eti Rohaeti Mpd
Basic English for Communication - Dr Hj Euis Eti Rohaeti MpdBasic English for Communication - Dr Hj Euis Eti Rohaeti Mpd
Basic English for Communication - Dr Hj Euis Eti Rohaeti Mpd
Restu Bias Primandhika
 
How to Configure Vendor Management in Lunch App of Odoo 18
How to Configure Vendor Management in Lunch App of Odoo 18How to Configure Vendor Management in Lunch App of Odoo 18
How to Configure Vendor Management in Lunch App of Odoo 18
Celine George
 
How to Manage Upselling of Subscriptions in Odoo 18
How to Manage Upselling of Subscriptions in Odoo 18How to Manage Upselling of Subscriptions in Odoo 18
How to Manage Upselling of Subscriptions in Odoo 18
Celine George
 
Paper 108 | Thoreau’s Influence on Gandhi: The Evolution of Civil Disobedience
Paper 108 | Thoreau’s Influence on Gandhi: The Evolution of Civil DisobediencePaper 108 | Thoreau’s Influence on Gandhi: The Evolution of Civil Disobedience
Paper 108 | Thoreau’s Influence on Gandhi: The Evolution of Civil Disobedience
Rajdeep Bavaliya
 
Webcrawler_Mule_AIChain_MuleSoft_Meetup_Hyderabad
Webcrawler_Mule_AIChain_MuleSoft_Meetup_HyderabadWebcrawler_Mule_AIChain_MuleSoft_Meetup_Hyderabad
Webcrawler_Mule_AIChain_MuleSoft_Meetup_Hyderabad
Veera Pallapu
 
Unit 3 Poster Sketches with annotations.pptx
Unit 3 Poster Sketches with annotations.pptxUnit 3 Poster Sketches with annotations.pptx
Unit 3 Poster Sketches with annotations.pptx
bobby205207
 
ROLE PLAY: FIRST AID -CPR & RECOVERY POSITION.pptx
ROLE PLAY: FIRST AID -CPR & RECOVERY POSITION.pptxROLE PLAY: FIRST AID -CPR & RECOVERY POSITION.pptx
ROLE PLAY: FIRST AID -CPR & RECOVERY POSITION.pptx
Belicia R.S
 
ABCs of Bookkeeping for Nonprofits TechSoup.pdf
ABCs of Bookkeeping for Nonprofits TechSoup.pdfABCs of Bookkeeping for Nonprofits TechSoup.pdf
ABCs of Bookkeeping for Nonprofits TechSoup.pdf
TechSoup
 
MATERI PPT TOPIK 1 LANDASAN FILOSOFIS PENDIDIKAN
MATERI PPT TOPIK 1 LANDASAN FILOSOFIS PENDIDIKANMATERI PPT TOPIK 1 LANDASAN FILOSOFIS PENDIDIKAN
MATERI PPT TOPIK 1 LANDASAN FILOSOFIS PENDIDIKAN
aditya23173
 
Energy Balances Of Oecd Countries 2011 Iea Statistics 1st Edition Oecd
Energy Balances Of Oecd Countries 2011 Iea Statistics 1st Edition OecdEnergy Balances Of Oecd Countries 2011 Iea Statistics 1st Edition Oecd
Energy Balances Of Oecd Countries 2011 Iea Statistics 1st Edition Oecd
razelitouali
 
MATERI PPT TOPIK 4 LANDASAN FILOSOFIS PENDIDIKAN
MATERI PPT TOPIK 4 LANDASAN FILOSOFIS PENDIDIKANMATERI PPT TOPIK 4 LANDASAN FILOSOFIS PENDIDIKAN
MATERI PPT TOPIK 4 LANDASAN FILOSOFIS PENDIDIKAN
aditya23173
 

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