SlideShare une entreprise Scribd logo
1  sur  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

Contenu connexe

Tendances

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
 

Tendances (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
 

Similaire à 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
 

Similaire à 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
 

Plus de monikadeshmane

Plus de 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
 

Dernier

Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104misteraugie
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...EduSkills OECD
 
Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Disha Kariya
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeThiyagu K
 
Making and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdfMaking and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdfChris Hunter
 
PROCESS RECORDING FORMAT.docx
PROCESS      RECORDING        FORMAT.docxPROCESS      RECORDING        FORMAT.docx
PROCESS RECORDING FORMAT.docxPoojaSen20
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxAreebaZafar22
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxVishalSingh1417
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphThiyagu K
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAssociation for Project Management
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdfQucHHunhnh
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfAdmir Softic
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxDenish Jangid
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxiammrhaywood
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.pptRamjanShidvankar
 
Seal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxSeal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxnegromaestrong
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdfQucHHunhnh
 
SECOND SEMESTER TOPIC COVERAGE SY 2023-2024 Trends, Networks, and Critical Th...
SECOND SEMESTER TOPIC COVERAGE SY 2023-2024 Trends, Networks, and Critical Th...SECOND SEMESTER TOPIC COVERAGE SY 2023-2024 Trends, Networks, and Critical Th...
SECOND SEMESTER TOPIC COVERAGE SY 2023-2024 Trends, Networks, and Critical Th...KokoStevan
 
psychiatric nursing HISTORY COLLECTION .docx
psychiatric  nursing HISTORY  COLLECTION  .docxpsychiatric  nursing HISTORY  COLLECTION  .docx
psychiatric nursing HISTORY COLLECTION .docxPoojaSen20
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactdawncurless
 

Dernier (20)

Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
 
Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and Mode
 
Making and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdfMaking and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdf
 
PROCESS RECORDING FORMAT.docx
PROCESS      RECORDING        FORMAT.docxPROCESS      RECORDING        FORMAT.docx
PROCESS RECORDING FORMAT.docx
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptx
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptx
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot Graph
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across Sectors
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.ppt
 
Seal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxSeal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptx
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
 
SECOND SEMESTER TOPIC COVERAGE SY 2023-2024 Trends, Networks, and Critical Th...
SECOND SEMESTER TOPIC COVERAGE SY 2023-2024 Trends, Networks, and Critical Th...SECOND SEMESTER TOPIC COVERAGE SY 2023-2024 Trends, Networks, and Critical Th...
SECOND SEMESTER TOPIC COVERAGE SY 2023-2024 Trends, Networks, and Critical Th...
 
psychiatric nursing HISTORY COLLECTION .docx
psychiatric  nursing HISTORY  COLLECTION  .docxpsychiatric  nursing HISTORY  COLLECTION  .docx
psychiatric nursing HISTORY COLLECTION .docx
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impact
 

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