SlideShare a Scribd company logo
1 of 20
Strings
TYBSc. Comp. Sci.
Chapter 2- Functions & Strings
monica deshmane(H.V.Desai college) 1
Points to learn
String
• Regular expressions
• POSIX
• Perl Compatible
monica deshmane(H.V.Desai college) 2
monica deshmane(H.V.Desai college)
3
1. Matching
$b = ereg(pattern, string [,captured]);
Depending on match it returns count for number of
matches and also populates the array.
echo "<br>".ereg('c(.*)e$','Welcome', $arr);
echo "<br>";
print_r($arr);
Output:
4
Array ( [0] => come [1] => om )
0th element is set to the entire string being matched and 1st element is substring that matched.
Regular Expression
1.POSIX
Uses of POSIX Functions
monica deshmane(H.V.Desai college) 4
$email_id = "admin@tutorialspoint.com";
$retval =preg_match("/.com$/", $email_id);
if( $retval == true )
{
echo "Found a .com<br>";
} else {
echo "Could not found a .com<br>";
}
Try this-
monica deshmane(H.V.Desai college) 5
<?php
$email = "abc123@sdsd.com";
$regex = '/^([a-z0-9])+@([a-z0-9])+.([a-z]{2,3})$/';
if (preg_match($regex, $email)) {
echo $email . " is a valid email. We can accept it.";
} else {
echo $email . " is an invalid email. Please try again.";
}
?>
Email validation
monica deshmane(H.V.Desai college)
6
2.Replacing
$str = ereg_replace(pattern, replacement, string)
The ereg_replace() function searches for string
specified by pattern and replaces pattern with
replacement if found.
Like ereg(), ereg_replace() is case sensitive.
eregi_replace is case insesnsitive.
It returns the modified string, but if no matches are
found, the string will remain unchanged.
e.g.
$copy_date = “june1999";
$copy_date1 = ereg_replace("([0-9]+)", "2000", $copy_date);
print $copy_date1;
Output: june2000
Regular Expression
1.POSIX
Uses of POSIX Functions
monica deshmane(H.V.Desai college) 7
<?php
$s="hi , php, 2";
$arr=preg_split("/,/",$s);
print_r($arr);
?>
3. splitting
monica deshmane(H.V.Desai college) 8
Similarly ,
Try for
1) Extract information-
matching- Preg_match()
2) Substitution-
replacing- Preg_replace()
3) Splitting- Preg_split()
monica deshmane(H.V.Desai college)
9
1. Searching pattern at the start of string(^)
$b = ereg('^We', 'Welcome to PHP Program');
echo $b;
2. Searching pattern at the end of string($)
$b = ereg('We$', 'Welcome to PHP Program');
echo $b;
3. Dot (.)/period operator Used to match single character
echo "<br>".ereg('n.t', 'This is not a word'); //1
echo "<br>".ereg('n.t', 'nut is not here'); //1
echo "<br>".ereg('n.t', 'Take this bat'); //0
echo "<br>".ereg('n.t', 'n t'); //1
echo "<br>".ereg('n.t', 'nt'); //0
Regular Expression
1.POSIX Basics of Regular Expression
monica deshmane(H.V.Desai college)
10
3. Splitting
$arr = ereg_split(pattern, string[, limit])
The split() function will divide a string into various elements,
the boundaries of each element based on the occurrence of pattern in string.
The optional parameter limit is used to show the number of elements
into which the string should be divided,
starting from the left end of the string and working rightward.
In cases where the pattern is an alphabetical character, split() is case sensitive.
It returns an array of strings after splitting up a string.
Regular Expression
1.POSIX
Uses of POSIX Functions
monica deshmane(H.V.Desai college)
11
4. Matching special character /meta character use backslash or escape.
echo ereg(‘$50.00', 'Bill amount is $50.00'); //1
echo ereg(‘$50.00', 'Bill amount is $50.00'); //0
RE are case sensitive by default.
To perform case insensitive use eregi() used.
Abstract patterns-
1)Character class
2)Alternatives
3)Repeating sequences
Regular Expression
1.POSIX Basics of Regular Expression
monica deshmane(H.V.Desai college)
12
Character class is used to specify set of acceptable
characters in pattern.
defined by enclosing the acceptable characters in square bracket.
• Acceptable characters are-alphabets,numeric,punctuation marks.
• alphabets
echo "<br>".ereg('ch[aeiou]t', 'This is online chat'); //1
echo "<br>".ereg('ch[aeiou]t', 'See the chit'); //1
echo "<br>".ereg('ch[aeiou]t', ‘charitable'); //0
Regular Expression
Abstract patterns 1) Character classes
monica deshmane(H.V.Desai college)
13
•negate/except
Specify the ^ sign at the start of character class, this will negate/except the condition.
echo "<br>".ereg('ch[^aeiou]t', 'This is online chat'); //0
echo "<br>".ereg('ch[^aeiou]t', 'See the chit'); //0
echo "<br>".ereg('ch[^aeiou]t', 'chrt'); //1
• Use hypen(-) to specify the range of characters.
echo "<br>".ereg('[0-9]th', ‘This number is 10th'); //1
echo "<br>".ereg('[0123456789]th', This number is 7th'); //1
echo "<br>".ereg('[a-z]e', 'Welcome to internet Programming'); //1
echo "<br>".ereg('[a-z][A-Z]e', 'Welcome to internet Programming '); //1
Regular Expression
Abstract patterns
1) Character classes
monica deshmane(H.V.Desai college)
14
Character classes
[:alnum:] Alphanumeric characters [0-9a-zA-Z]
[:alpha:] Alphabetic characters [a-zA-Z]
[:blank:] space and tab [ t]
[:digit:] Digits [0-9]
[:lower:] Lower case letters [a-z]
[:upper:] Uppercase letters [A-Z]
[:xdigit:] Hexadecimal digit [0-9a-fA-F]
range of characters ex. [‘aeiou’]
Range ex. [2,6]
 for special character Ex . [‘$’]
echo ereg('[[:digit:]]','23432434'); //1
echo ereg(‘[0-9]','23432434'); //1
Regular Expression
Abstract patterns 1) Character classes
monica deshmane(H.V.Desai college)
15
Vertical Pipe(|) symbol is used to specify alternatives.
echo "<br>".ereg('Hi|Hello', 'Welcome, Hello guys'); //1
echo "<br>".ereg('Hi|Hello', 'Welcome, Hi ameya'); //1
echo "<br>".ereg('Hi|Hello', 'Welcome, how are u?'); //0
String start with Hi or ends with Hello
echo "<br>".ereg('^Hi|Hello$', 'Hi, are u there?'); //1
echo "<br>".ereg('^Hi|Hello$', 'Hello, are u fine?'); //0
echo "<br>".ereg('^Hi|Hello$', 'how do u do?, Hello');//1
Regular Expression
Abstract patterns 2) Alternatives
monica deshmane(H.V.Desai college)
16
? 0 or 1
* 0 or more
+ 1 or more
{n} Exactly n times
{n,m} Atleast n, no more than m times
{n,} Atleast n times
echo "<br>".ereg('Hel+o', 'Hellllo'); //1
echo "<br>".ereg('Hel+o', 'Helo'); //0
echo "<br>".ereg('Hel?o', 'Helo'); //1
echo "<br>".ereg('Hel*o', 'Hellllo'); //1
echo "<br>".ereg('[0-9]{3}-[0-9]{3}-[0-9]{4}', '243-543-3567‘); //1
Regular Expression
Abstract patterns
3) Repeating Sequences
Subpatterns
echo "<br>".ereg('a (very )+good', 'Its a very very
good idea'); //1
Parentheses is used to group bits of regular
expression.
monica deshmane(H.V.Desai college) 17
preg_grep
preg_grep ( string $pattern , array $input [, int $flags = 0 ] ) :
Return array entries that match the pattern
Returns an array indexed using the keys from the input array.
$input = [ "Redish", "Pinkish", "Greenish","Purple"];
$result = preg_grep("/^p/i", $input);
print_r($result);
?>
monica deshmane(H.V.Desai college) 18
perl compatible RE-
1.preg_match()-matches 1st match to pattern.
2.preg_match_all()-all matches.
3.preg_replace()
4. preg_split()
5.Preg_grep()
6.preg_quote()
monica deshmane(H.V.Desai college) 19
Uses
End of Presentation
monica deshmane(H.V.Desai college) 20

More Related Content

What's hot

Round PEG, Round Hole - Parsing Functionally
Round PEG, Round Hole - Parsing FunctionallyRound PEG, Round Hole - Parsing Functionally
Round PEG, Round Hole - Parsing FunctionallySean Cribbs
 
Slides chapter3part1 ruby-forjavaprogrammers
Slides chapter3part1 ruby-forjavaprogrammersSlides chapter3part1 ruby-forjavaprogrammers
Slides chapter3part1 ruby-forjavaprogrammersGiovanni924
 
Advanced Python, Part 2
Advanced Python, Part 2Advanced Python, Part 2
Advanced Python, Part 2Zaar Hai
 
Separation of Concerns in Language Definition
Separation of Concerns in Language DefinitionSeparation of Concerns in Language Definition
Separation of Concerns in Language DefinitionEelco Visser
 
Erlang/OTP for Rubyists
Erlang/OTP for RubyistsErlang/OTP for Rubyists
Erlang/OTP for RubyistsSean Cribbs
 
A Language Designer’s Workbench. A one-stop shop for implementation and verif...
A Language Designer’s Workbench. A one-stop shop for implementation and verif...A Language Designer’s Workbench. A one-stop shop for implementation and verif...
A Language Designer’s Workbench. A one-stop shop for implementation and verif...Eelco Visser
 
Creating Domain Specific Languages in Python
Creating Domain Specific Languages in PythonCreating Domain Specific Languages in Python
Creating Domain Specific Languages in PythonSiddhi
 
Declare Your Language: Type Checking
Declare Your Language: Type CheckingDeclare Your Language: Type Checking
Declare Your Language: Type CheckingEelco Visser
 
Dynamic Semantics Specification and Interpreter Generation
Dynamic Semantics Specification and Interpreter GenerationDynamic Semantics Specification and Interpreter Generation
Dynamic Semantics Specification and Interpreter GenerationEelco Visser
 
Python Puzzlers - 2016 Edition
Python Puzzlers - 2016 EditionPython Puzzlers - 2016 Edition
Python Puzzlers - 2016 EditionNandan Sawant
 
Object Orientation vs Functional Programming in Python
Object Orientation vs Functional Programming in PythonObject Orientation vs Functional Programming in Python
Object Orientation vs Functional Programming in PythonTendayi Mawushe
 
Regular Expressions in PHP
Regular Expressions in PHPRegular Expressions in PHP
Regular Expressions in PHPAndrew Kandels
 
String and string manipulation x
String and string manipulation xString and string manipulation x
String and string manipulation xShahjahan Samoon
 
Introduction to Python for Plone developers
Introduction to Python for Plone developersIntroduction to Python for Plone developers
Introduction to Python for Plone developersJim Roepcke
 
String and string manipulation
String and string manipulationString and string manipulation
String and string manipulationShahjahan Samoon
 
PHP Functions & Arrays
PHP Functions & ArraysPHP Functions & Arrays
PHP Functions & ArraysHenry Osborne
 

What's hot (20)

Round PEG, Round Hole - Parsing Functionally
Round PEG, Round Hole - Parsing FunctionallyRound PEG, Round Hole - Parsing Functionally
Round PEG, Round Hole - Parsing Functionally
 
Slides chapter3part1 ruby-forjavaprogrammers
Slides chapter3part1 ruby-forjavaprogrammersSlides chapter3part1 ruby-forjavaprogrammers
Slides chapter3part1 ruby-forjavaprogrammers
 
Advanced Python, Part 2
Advanced Python, Part 2Advanced Python, Part 2
Advanced Python, Part 2
 
Separation of Concerns in Language Definition
Separation of Concerns in Language DefinitionSeparation of Concerns in Language Definition
Separation of Concerns in Language Definition
 
Erlang/OTP for Rubyists
Erlang/OTP for RubyistsErlang/OTP for Rubyists
Erlang/OTP for Rubyists
 
Strings in C
Strings in CStrings in C
Strings in C
 
A Language Designer’s Workbench. A one-stop shop for implementation and verif...
A Language Designer’s Workbench. A one-stop shop for implementation and verif...A Language Designer’s Workbench. A one-stop shop for implementation and verif...
A Language Designer’s Workbench. A one-stop shop for implementation and verif...
 
Creating Domain Specific Languages in Python
Creating Domain Specific Languages in PythonCreating Domain Specific Languages in Python
Creating Domain Specific Languages in Python
 
Working with text, Regular expressions
Working with text, Regular expressionsWorking with text, Regular expressions
Working with text, Regular expressions
 
Declare Your Language: Type Checking
Declare Your Language: Type CheckingDeclare Your Language: Type Checking
Declare Your Language: Type Checking
 
Dynamic Semantics Specification and Interpreter Generation
Dynamic Semantics Specification and Interpreter GenerationDynamic Semantics Specification and Interpreter Generation
Dynamic Semantics Specification and Interpreter Generation
 
Python Puzzlers - 2016 Edition
Python Puzzlers - 2016 EditionPython Puzzlers - 2016 Edition
Python Puzzlers - 2016 Edition
 
Hashes
HashesHashes
Hashes
 
Object Orientation vs Functional Programming in Python
Object Orientation vs Functional Programming in PythonObject Orientation vs Functional Programming in Python
Object Orientation vs Functional Programming in Python
 
Regular Expressions in PHP
Regular Expressions in PHPRegular Expressions in PHP
Regular Expressions in PHP
 
String and string manipulation x
String and string manipulation xString and string manipulation x
String and string manipulation x
 
Introduction to Python for Plone developers
Introduction to Python for Plone developersIntroduction to Python for Plone developers
Introduction to Python for Plone developers
 
String and string manipulation
String and string manipulationString and string manipulation
String and string manipulation
 
Strings
StringsStrings
Strings
 
PHP Functions & Arrays
PHP Functions & ArraysPHP Functions & Arrays
PHP Functions & Arrays
 

Similar to php string part 4

Regular expressions in oracle
Regular expressions in oracleRegular expressions in oracle
Regular expressions in oracleLogan Palanisamy
 
Regular_Expressions.pptx
Regular_Expressions.pptxRegular_Expressions.pptx
Regular_Expressions.pptxDurgaNayak4
 
Php Chapter 4 Training
Php Chapter 4 TrainingPhp Chapter 4 Training
Php Chapter 4 TrainingChris Chubb
 
Maxbox starter20
Maxbox starter20Maxbox starter20
Maxbox starter20Max Kleiner
 
Regular expressions in Python
Regular expressions in PythonRegular expressions in Python
Regular expressions in PythonSujith Kumar
 
Achieving Parsing Sanity In Erlang
Achieving Parsing Sanity In ErlangAchieving Parsing Sanity In Erlang
Achieving Parsing Sanity In ErlangSean Cribbs
 
9 character string &amp; string library
9  character string &amp; string library9  character string &amp; string library
9 character string &amp; string libraryMomenMostafa
 
Python Workshop - Learn Python the Hard Way
Python Workshop - Learn Python the Hard WayPython Workshop - Learn Python the Hard Way
Python Workshop - Learn Python the Hard WayUtkarsh Sengar
 
Introduction to Regular Expressions
Introduction to Regular ExpressionsIntroduction to Regular Expressions
Introduction to Regular ExpressionsJesse Anderson
 
07. Java Array, Set and Maps
07.  Java Array, Set and Maps07.  Java Array, Set and Maps
07. Java Array, Set and MapsIntro C# Book
 
Ruby presentasjon på NTNU 22 april 2009
Ruby presentasjon på NTNU 22 april 2009Ruby presentasjon på NTNU 22 april 2009
Ruby presentasjon på NTNU 22 april 2009Aslak Hellesøy
 
Ruby presentasjon på NTNU 22 april 2009
Ruby presentasjon på NTNU 22 april 2009Ruby presentasjon på NTNU 22 april 2009
Ruby presentasjon på NTNU 22 april 2009Aslak Hellesøy
 
Ruby presentasjon på NTNU 22 april 2009
Ruby presentasjon på NTNU 22 april 2009Ruby presentasjon på NTNU 22 april 2009
Ruby presentasjon på NTNU 22 april 2009Aslak Hellesøy
 
Bioinformatica 06-10-2011-p2 introduction
Bioinformatica 06-10-2011-p2 introductionBioinformatica 06-10-2011-p2 introduction
Bioinformatica 06-10-2011-p2 introductionProf. Wim Van Criekinge
 

Similar to php string part 4 (20)

Regular expressions in oracle
Regular expressions in oracleRegular expressions in oracle
Regular expressions in oracle
 
Regular_Expressions.pptx
Regular_Expressions.pptxRegular_Expressions.pptx
Regular_Expressions.pptx
 
Php Chapter 4 Training
Php Chapter 4 TrainingPhp Chapter 4 Training
Php Chapter 4 Training
 
Maxbox starter20
Maxbox starter20Maxbox starter20
Maxbox starter20
 
Regular expression for everyone
Regular expression for everyoneRegular expression for everyone
Regular expression for everyone
 
P3 2018 python_regexes
P3 2018 python_regexesP3 2018 python_regexes
P3 2018 python_regexes
 
First steps in C-Shell
First steps in C-ShellFirst steps in C-Shell
First steps in C-Shell
 
Python lecture 05
Python lecture 05Python lecture 05
Python lecture 05
 
Regular expressions in Python
Regular expressions in PythonRegular expressions in Python
Regular expressions in Python
 
Achieving Parsing Sanity In Erlang
Achieving Parsing Sanity In ErlangAchieving Parsing Sanity In Erlang
Achieving Parsing Sanity In Erlang
 
Bioinformatica p2-p3-introduction
Bioinformatica p2-p3-introductionBioinformatica p2-p3-introduction
Bioinformatica p2-p3-introduction
 
9 character string &amp; string library
9  character string &amp; string library9  character string &amp; string library
9 character string &amp; string library
 
Python Workshop - Learn Python the Hard Way
Python Workshop - Learn Python the Hard WayPython Workshop - Learn Python the Hard Way
Python Workshop - Learn Python the Hard Way
 
P3 2017 python_regexes
P3 2017 python_regexesP3 2017 python_regexes
P3 2017 python_regexes
 
Introduction to Regular Expressions
Introduction to Regular ExpressionsIntroduction to Regular Expressions
Introduction to Regular Expressions
 
07. Java Array, Set and Maps
07.  Java Array, Set and Maps07.  Java Array, Set and Maps
07. Java Array, Set and Maps
 
Ruby presentasjon på NTNU 22 april 2009
Ruby presentasjon på NTNU 22 april 2009Ruby presentasjon på NTNU 22 april 2009
Ruby presentasjon på NTNU 22 april 2009
 
Ruby presentasjon på NTNU 22 april 2009
Ruby presentasjon på NTNU 22 april 2009Ruby presentasjon på NTNU 22 april 2009
Ruby presentasjon på NTNU 22 april 2009
 
Ruby presentasjon på NTNU 22 april 2009
Ruby presentasjon på NTNU 22 april 2009Ruby presentasjon på NTNU 22 april 2009
Ruby presentasjon på NTNU 22 april 2009
 
Bioinformatica 06-10-2011-p2 introduction
Bioinformatica 06-10-2011-p2 introductionBioinformatica 06-10-2011-p2 introduction
Bioinformatica 06-10-2011-p2 introduction
 

More from monikadeshmane

More from monikadeshmane (19)

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

Recently uploaded

Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxthorishapillay1
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxOH TEIK BIN
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxpboyjonauth
 
Romantic Opera MUSIC FOR GRADE NINE pptx
Romantic Opera MUSIC FOR GRADE NINE pptxRomantic Opera MUSIC FOR GRADE NINE pptx
Romantic Opera MUSIC FOR GRADE NINE pptxsqpmdrvczh
 
Types of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptxTypes of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptxEyham Joco
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Educationpboyjonauth
 
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxEPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxRaymartEstabillo3
 
Full Stack Web Development Course for Beginners
Full Stack Web Development Course  for BeginnersFull Stack Web Development Course  for Beginners
Full Stack Web Development Course for BeginnersSabitha Banu
 
AmericanHighSchoolsprezentacijaoskolama.
AmericanHighSchoolsprezentacijaoskolama.AmericanHighSchoolsprezentacijaoskolama.
AmericanHighSchoolsprezentacijaoskolama.arsicmarija21
 
Quarter 4 Peace-education.pptx Catch Up Friday
Quarter 4 Peace-education.pptx Catch Up FridayQuarter 4 Peace-education.pptx Catch Up Friday
Quarter 4 Peace-education.pptx Catch Up FridayMakMakNepo
 
Judging the Relevance and worth of ideas part 2.pptx
Judging the Relevance  and worth of ideas part 2.pptxJudging the Relevance  and worth of ideas part 2.pptx
Judging the Relevance and worth of ideas part 2.pptxSherlyMaeNeri
 
Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17Celine George
 
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptxMULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptxAnupkumar Sharma
 
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...Nguyen Thanh Tu Collection
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon AUnboundStockton
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTiammrhaywood
 
ROOT CAUSE ANALYSIS PowerPoint Presentation
ROOT CAUSE ANALYSIS PowerPoint PresentationROOT CAUSE ANALYSIS PowerPoint Presentation
ROOT CAUSE ANALYSIS PowerPoint PresentationAadityaSharma884161
 
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxiammrhaywood
 

Recently uploaded (20)

Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptx
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptx
 
OS-operating systems- ch04 (Threads) ...
OS-operating systems- ch04 (Threads) ...OS-operating systems- ch04 (Threads) ...
OS-operating systems- ch04 (Threads) ...
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptx
 
Romantic Opera MUSIC FOR GRADE NINE pptx
Romantic Opera MUSIC FOR GRADE NINE pptxRomantic Opera MUSIC FOR GRADE NINE pptx
Romantic Opera MUSIC FOR GRADE NINE pptx
 
Types of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptxTypes of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptx
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Education
 
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxEPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
 
Full Stack Web Development Course for Beginners
Full Stack Web Development Course  for BeginnersFull Stack Web Development Course  for Beginners
Full Stack Web Development Course for Beginners
 
AmericanHighSchoolsprezentacijaoskolama.
AmericanHighSchoolsprezentacijaoskolama.AmericanHighSchoolsprezentacijaoskolama.
AmericanHighSchoolsprezentacijaoskolama.
 
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
 
Quarter 4 Peace-education.pptx Catch Up Friday
Quarter 4 Peace-education.pptx Catch Up FridayQuarter 4 Peace-education.pptx Catch Up Friday
Quarter 4 Peace-education.pptx Catch Up Friday
 
Judging the Relevance and worth of ideas part 2.pptx
Judging the Relevance  and worth of ideas part 2.pptxJudging the Relevance  and worth of ideas part 2.pptx
Judging the Relevance and worth of ideas part 2.pptx
 
Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17
 
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptxMULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
 
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon A
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
 
ROOT CAUSE ANALYSIS PowerPoint Presentation
ROOT CAUSE ANALYSIS PowerPoint PresentationROOT CAUSE ANALYSIS PowerPoint Presentation
ROOT CAUSE ANALYSIS PowerPoint Presentation
 
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
 

php string part 4

  • 1. Strings TYBSc. Comp. Sci. Chapter 2- Functions & Strings monica deshmane(H.V.Desai college) 1
  • 2. Points to learn String • Regular expressions • POSIX • Perl Compatible monica deshmane(H.V.Desai college) 2
  • 3. monica deshmane(H.V.Desai college) 3 1. Matching $b = ereg(pattern, string [,captured]); Depending on match it returns count for number of matches and also populates the array. echo "<br>".ereg('c(.*)e$','Welcome', $arr); echo "<br>"; print_r($arr); Output: 4 Array ( [0] => come [1] => om ) 0th element is set to the entire string being matched and 1st element is substring that matched. Regular Expression 1.POSIX Uses of POSIX Functions
  • 4. monica deshmane(H.V.Desai college) 4 $email_id = "admin@tutorialspoint.com"; $retval =preg_match("/.com$/", $email_id); if( $retval == true ) { echo "Found a .com<br>"; } else { echo "Could not found a .com<br>"; } Try this-
  • 5. monica deshmane(H.V.Desai college) 5 <?php $email = "abc123@sdsd.com"; $regex = '/^([a-z0-9])+@([a-z0-9])+.([a-z]{2,3})$/'; if (preg_match($regex, $email)) { echo $email . " is a valid email. We can accept it."; } else { echo $email . " is an invalid email. Please try again."; } ?> Email validation
  • 6. monica deshmane(H.V.Desai college) 6 2.Replacing $str = ereg_replace(pattern, replacement, string) The ereg_replace() function searches for string specified by pattern and replaces pattern with replacement if found. Like ereg(), ereg_replace() is case sensitive. eregi_replace is case insesnsitive. It returns the modified string, but if no matches are found, the string will remain unchanged. e.g. $copy_date = “june1999"; $copy_date1 = ereg_replace("([0-9]+)", "2000", $copy_date); print $copy_date1; Output: june2000 Regular Expression 1.POSIX Uses of POSIX Functions
  • 7. monica deshmane(H.V.Desai college) 7 <?php $s="hi , php, 2"; $arr=preg_split("/,/",$s); print_r($arr); ?> 3. splitting
  • 8. monica deshmane(H.V.Desai college) 8 Similarly , Try for 1) Extract information- matching- Preg_match() 2) Substitution- replacing- Preg_replace() 3) Splitting- Preg_split()
  • 9. monica deshmane(H.V.Desai college) 9 1. Searching pattern at the start of string(^) $b = ereg('^We', 'Welcome to PHP Program'); echo $b; 2. Searching pattern at the end of string($) $b = ereg('We$', 'Welcome to PHP Program'); echo $b; 3. Dot (.)/period operator Used to match single character echo "<br>".ereg('n.t', 'This is not a word'); //1 echo "<br>".ereg('n.t', 'nut is not here'); //1 echo "<br>".ereg('n.t', 'Take this bat'); //0 echo "<br>".ereg('n.t', 'n t'); //1 echo "<br>".ereg('n.t', 'nt'); //0 Regular Expression 1.POSIX Basics of Regular Expression
  • 10. monica deshmane(H.V.Desai college) 10 3. Splitting $arr = ereg_split(pattern, string[, limit]) The split() function will divide a string into various elements, the boundaries of each element based on the occurrence of pattern in string. The optional parameter limit is used to show the number of elements into which the string should be divided, starting from the left end of the string and working rightward. In cases where the pattern is an alphabetical character, split() is case sensitive. It returns an array of strings after splitting up a string. Regular Expression 1.POSIX Uses of POSIX Functions
  • 11. monica deshmane(H.V.Desai college) 11 4. Matching special character /meta character use backslash or escape. echo ereg(‘$50.00', 'Bill amount is $50.00'); //1 echo ereg(‘$50.00', 'Bill amount is $50.00'); //0 RE are case sensitive by default. To perform case insensitive use eregi() used. Abstract patterns- 1)Character class 2)Alternatives 3)Repeating sequences Regular Expression 1.POSIX Basics of Regular Expression
  • 12. monica deshmane(H.V.Desai college) 12 Character class is used to specify set of acceptable characters in pattern. defined by enclosing the acceptable characters in square bracket. • Acceptable characters are-alphabets,numeric,punctuation marks. • alphabets echo "<br>".ereg('ch[aeiou]t', 'This is online chat'); //1 echo "<br>".ereg('ch[aeiou]t', 'See the chit'); //1 echo "<br>".ereg('ch[aeiou]t', ‘charitable'); //0 Regular Expression Abstract patterns 1) Character classes
  • 13. monica deshmane(H.V.Desai college) 13 •negate/except Specify the ^ sign at the start of character class, this will negate/except the condition. echo "<br>".ereg('ch[^aeiou]t', 'This is online chat'); //0 echo "<br>".ereg('ch[^aeiou]t', 'See the chit'); //0 echo "<br>".ereg('ch[^aeiou]t', 'chrt'); //1 • Use hypen(-) to specify the range of characters. echo "<br>".ereg('[0-9]th', ‘This number is 10th'); //1 echo "<br>".ereg('[0123456789]th', This number is 7th'); //1 echo "<br>".ereg('[a-z]e', 'Welcome to internet Programming'); //1 echo "<br>".ereg('[a-z][A-Z]e', 'Welcome to internet Programming '); //1 Regular Expression Abstract patterns 1) Character classes
  • 14. monica deshmane(H.V.Desai college) 14 Character classes [:alnum:] Alphanumeric characters [0-9a-zA-Z] [:alpha:] Alphabetic characters [a-zA-Z] [:blank:] space and tab [ t] [:digit:] Digits [0-9] [:lower:] Lower case letters [a-z] [:upper:] Uppercase letters [A-Z] [:xdigit:] Hexadecimal digit [0-9a-fA-F] range of characters ex. [‘aeiou’] Range ex. [2,6] for special character Ex . [‘$’] echo ereg('[[:digit:]]','23432434'); //1 echo ereg(‘[0-9]','23432434'); //1 Regular Expression Abstract patterns 1) Character classes
  • 15. monica deshmane(H.V.Desai college) 15 Vertical Pipe(|) symbol is used to specify alternatives. echo "<br>".ereg('Hi|Hello', 'Welcome, Hello guys'); //1 echo "<br>".ereg('Hi|Hello', 'Welcome, Hi ameya'); //1 echo "<br>".ereg('Hi|Hello', 'Welcome, how are u?'); //0 String start with Hi or ends with Hello echo "<br>".ereg('^Hi|Hello$', 'Hi, are u there?'); //1 echo "<br>".ereg('^Hi|Hello$', 'Hello, are u fine?'); //0 echo "<br>".ereg('^Hi|Hello$', 'how do u do?, Hello');//1 Regular Expression Abstract patterns 2) Alternatives
  • 16. monica deshmane(H.V.Desai college) 16 ? 0 or 1 * 0 or more + 1 or more {n} Exactly n times {n,m} Atleast n, no more than m times {n,} Atleast n times echo "<br>".ereg('Hel+o', 'Hellllo'); //1 echo "<br>".ereg('Hel+o', 'Helo'); //0 echo "<br>".ereg('Hel?o', 'Helo'); //1 echo "<br>".ereg('Hel*o', 'Hellllo'); //1 echo "<br>".ereg('[0-9]{3}-[0-9]{3}-[0-9]{4}', '243-543-3567‘); //1 Regular Expression Abstract patterns 3) Repeating Sequences
  • 17. Subpatterns echo "<br>".ereg('a (very )+good', 'Its a very very good idea'); //1 Parentheses is used to group bits of regular expression. monica deshmane(H.V.Desai college) 17
  • 18. preg_grep preg_grep ( string $pattern , array $input [, int $flags = 0 ] ) : Return array entries that match the pattern Returns an array indexed using the keys from the input array. $input = [ "Redish", "Pinkish", "Greenish","Purple"]; $result = preg_grep("/^p/i", $input); print_r($result); ?> monica deshmane(H.V.Desai college) 18
  • 19. perl compatible RE- 1.preg_match()-matches 1st match to pattern. 2.preg_match_all()-all matches. 3.preg_replace() 4. preg_split() 5.Preg_grep() 6.preg_quote() monica deshmane(H.V.Desai college) 19 Uses
  • 20. End of Presentation monica deshmane(H.V.Desai college) 20