SlideShare a Scribd company logo
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
Ad

Recommended

Intoduction to php strings
Intoduction to php strings
baabtra.com - No. 1 supplier of quality freshers
 
Php basics
Php basics
hamfu
 
PHP Strings and Patterns
PHP Strings and Patterns
Henry Osborne
 
String variable in php
String variable in php
chantholnet
 
Class 5 - PHP Strings
Class 5 - PHP Strings
Ahmed Swilam
 
php string part 3
php string part 3
monikadeshmane
 
Advanced Python, Part 1
Advanced Python, Part 1
Zaar Hai
 
PHP string-part 1
PHP string-part 1
monikadeshmane
 
Round PEG, Round Hole - Parsing Functionally
Round PEG, Round Hole - Parsing Functionally
Sean Cribbs
 
Slides chapter3part1 ruby-forjavaprogrammers
Slides chapter3part1 ruby-forjavaprogrammers
Giovanni924
 
Advanced Python, Part 2
Advanced Python, Part 2
Zaar Hai
 
Separation of Concerns in Language Definition
Separation of Concerns in Language Definition
Eelco Visser
 
Erlang/OTP for Rubyists
Erlang/OTP for Rubyists
Sean 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...
Eelco Visser
 
Creating Domain Specific Languages in Python
Creating Domain Specific Languages in Python
Siddhi
 
Working with text, Regular expressions
Working with text, Regular expressions
Krasimir Berov (Красимир Беров)
 
Declare Your Language: Type Checking
Declare Your Language: Type Checking
Eelco Visser
 
Dynamic Semantics Specification and Interpreter Generation
Dynamic Semantics Specification and Interpreter Generation
Eelco Visser
 
Python Puzzlers - 2016 Edition
Python Puzzlers - 2016 Edition
Nandan Sawant
 
Hashes
Hashes
Krasimir Berov (Красимир Беров)
 
Object Orientation vs Functional Programming in Python
Object Orientation vs Functional Programming in Python
Tendayi Mawushe
 
Regular Expressions in PHP
Regular Expressions in PHP
Andrew Kandels
 
String and string manipulation x
String and string manipulation x
Shahjahan Samoon
 
Introduction to Python for Plone developers
Introduction to Python for Plone developers
Jim Roepcke
 
String and string manipulation
String and string manipulation
Shahjahan Samoon
 
Strings
Strings
Imad Ali
 
PHP Functions & Arrays
PHP Functions & Arrays
Henry Osborne
 
Regular expressions in oracle
Regular expressions in oracle
Logan Palanisamy
 
Regular_Expressions.pptx
Regular_Expressions.pptx
DurgaNayak4
 
Php Chapter 4 Training
Php Chapter 4 Training
Chris Chubb
 

More Related Content

What's hot (19)

Round PEG, Round Hole - Parsing Functionally
Round PEG, Round Hole - Parsing Functionally
Sean Cribbs
 
Slides chapter3part1 ruby-forjavaprogrammers
Slides chapter3part1 ruby-forjavaprogrammers
Giovanni924
 
Advanced Python, Part 2
Advanced Python, Part 2
Zaar Hai
 
Separation of Concerns in Language Definition
Separation of Concerns in Language Definition
Eelco Visser
 
Erlang/OTP for Rubyists
Erlang/OTP for Rubyists
Sean 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...
Eelco Visser
 
Creating Domain Specific Languages in Python
Creating Domain Specific Languages in Python
Siddhi
 
Working with text, Regular expressions
Working with text, Regular expressions
Krasimir Berov (Красимир Беров)
 
Declare Your Language: Type Checking
Declare Your Language: Type Checking
Eelco Visser
 
Dynamic Semantics Specification and Interpreter Generation
Dynamic Semantics Specification and Interpreter Generation
Eelco Visser
 
Python Puzzlers - 2016 Edition
Python Puzzlers - 2016 Edition
Nandan Sawant
 
Hashes
Hashes
Krasimir Berov (Красимир Беров)
 
Object Orientation vs Functional Programming in Python
Object Orientation vs Functional Programming in Python
Tendayi Mawushe
 
Regular Expressions in PHP
Regular Expressions in PHP
Andrew Kandels
 
String and string manipulation x
String and string manipulation x
Shahjahan Samoon
 
Introduction to Python for Plone developers
Introduction to Python for Plone developers
Jim Roepcke
 
String and string manipulation
String and string manipulation
Shahjahan Samoon
 
Strings
Strings
Imad Ali
 
PHP Functions & Arrays
PHP Functions & Arrays
Henry Osborne
 
Round PEG, Round Hole - Parsing Functionally
Round PEG, Round Hole - Parsing Functionally
Sean Cribbs
 
Slides chapter3part1 ruby-forjavaprogrammers
Slides chapter3part1 ruby-forjavaprogrammers
Giovanni924
 
Advanced Python, Part 2
Advanced Python, Part 2
Zaar Hai
 
Separation of Concerns in Language Definition
Separation of Concerns in Language Definition
Eelco Visser
 
Erlang/OTP for Rubyists
Erlang/OTP for Rubyists
Sean 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...
Eelco Visser
 
Creating Domain Specific Languages in Python
Creating Domain Specific Languages in Python
Siddhi
 
Declare Your Language: Type Checking
Declare Your Language: Type Checking
Eelco Visser
 
Dynamic Semantics Specification and Interpreter Generation
Dynamic Semantics Specification and Interpreter Generation
Eelco Visser
 
Python Puzzlers - 2016 Edition
Python Puzzlers - 2016 Edition
Nandan Sawant
 
Object Orientation vs Functional Programming in Python
Object Orientation vs Functional Programming in Python
Tendayi Mawushe
 
Regular Expressions in PHP
Regular Expressions in PHP
Andrew Kandels
 
String and string manipulation x
String and string manipulation x
Shahjahan Samoon
 
Introduction to Python for Plone developers
Introduction to Python for Plone developers
Jim Roepcke
 
String and string manipulation
String and string manipulation
Shahjahan Samoon
 
PHP Functions & Arrays
PHP Functions & Arrays
Henry Osborne
 

Similar to php string part 4 (20)

Regular expressions in oracle
Regular expressions in oracle
Logan Palanisamy
 
Regular_Expressions.pptx
Regular_Expressions.pptx
DurgaNayak4
 
Php Chapter 4 Training
Php Chapter 4 Training
Chris Chubb
 
Maxbox starter20
Maxbox starter20
Max Kleiner
 
Regular expression for everyone
Regular expression for everyone
Sanjeev Kumar Jaiswal
 
P3 2018 python_regexes
P3 2018 python_regexes
Prof. Wim Van Criekinge
 
First steps in C-Shell
First steps in C-Shell
Brahma Killampalli
 
Python lecture 05
Python lecture 05
Tanwir Zaman
 
Regular expressions in Python
Regular expressions in Python
Sujith Kumar
 
Achieving Parsing Sanity In Erlang
Achieving Parsing Sanity In Erlang
Sean Cribbs
 
Bioinformatica p2-p3-introduction
Bioinformatica p2-p3-introduction
Prof. Wim Van Criekinge
 
9 character string &amp; string library
9 character string &amp; string library
MomenMostafa
 
Python Workshop - Learn Python the Hard Way
Python Workshop - Learn Python the Hard Way
Utkarsh Sengar
 
P3 2017 python_regexes
P3 2017 python_regexes
Prof. Wim Van Criekinge
 
Introduction to Regular Expressions
Introduction to Regular Expressions
Jesse Anderson
 
07. Java Array, Set and Maps
07. Java Array, Set and Maps
Intro C# Book
 
/Regex makes me want to (weep_give up_(╯°□°)╯︵ ┻━┻)/i (for 2024 CascadiaPHP)
/Regex makes me want to (weep_give up_(╯°□°)╯︵ ┻━┻)/i (for 2024 CascadiaPHP)
brettflorio
 
unit-5 String Math Date Time AI presentation
unit-5 String Math Date Time AI presentation
MukeshTheLioner
 
Ruby presentasjon på NTNU 22 april 2009
Ruby presentasjon på NTNU 22 april 2009
Aslak Hellesøy
 
Ruby presentasjon på NTNU 22 april 2009
Ruby presentasjon på NTNU 22 april 2009
Aslak Hellesøy
 
Regular expressions in oracle
Regular expressions in oracle
Logan Palanisamy
 
Regular_Expressions.pptx
Regular_Expressions.pptx
DurgaNayak4
 
Php Chapter 4 Training
Php Chapter 4 Training
Chris Chubb
 
Maxbox starter20
Maxbox starter20
Max Kleiner
 
Regular expressions in Python
Regular expressions in Python
Sujith Kumar
 
Achieving Parsing Sanity In Erlang
Achieving Parsing Sanity In Erlang
Sean Cribbs
 
9 character string &amp; string library
9 character string &amp; string library
MomenMostafa
 
Python Workshop - Learn Python the Hard Way
Python Workshop - Learn Python the Hard Way
Utkarsh Sengar
 
Introduction to Regular Expressions
Introduction to Regular Expressions
Jesse Anderson
 
07. Java Array, Set and Maps
07. Java Array, Set and Maps
Intro C# Book
 
/Regex makes me want to (weep_give up_(╯°□°)╯︵ ┻━┻)/i (for 2024 CascadiaPHP)
/Regex makes me want to (weep_give up_(╯°□°)╯︵ ┻━┻)/i (for 2024 CascadiaPHP)
brettflorio
 
unit-5 String Math Date Time AI presentation
unit-5 String Math Date Time AI presentation
MukeshTheLioner
 
Ruby presentasjon på NTNU 22 april 2009
Ruby presentasjon på NTNU 22 april 2009
Aslak Hellesøy
 
Ruby presentasjon på NTNU 22 april 2009
Ruby presentasjon på NTNU 22 april 2009
Aslak Hellesøy
 
Ad

More from monikadeshmane (19)

File system node js
File system node js
monikadeshmane
 
Nodejs functions & modules
Nodejs functions & modules
monikadeshmane
 
Nodejs buffers
Nodejs buffers
monikadeshmane
 
Intsllation & 1st program nodejs
Intsllation & 1st program nodejs
monikadeshmane
 
Nodejs basics
Nodejs basics
monikadeshmane
 
Chap 5 php files part-2
Chap 5 php files part-2
monikadeshmane
 
Chap 5 php files part 1
Chap 5 php files part 1
monikadeshmane
 
Chap4 oop class (php) part 2
Chap4 oop class (php) part 2
monikadeshmane
 
Chap4 oop class (php) part 1
Chap4 oop class (php) part 1
monikadeshmane
 
Chap 3php array part4
Chap 3php array part4
monikadeshmane
 
Chap 3php array part 3
Chap 3php array part 3
monikadeshmane
 
Chap 3php array part 2
Chap 3php array part 2
monikadeshmane
 
Chap 3php array part1
Chap 3php array part1
monikadeshmane
 
PHP function
PHP function
monikadeshmane
 
php string-part 2
php string-part 2
monikadeshmane
 
java script
java script
monikadeshmane
 
ip1clientserver model
ip1clientserver model
monikadeshmane
 
Chap1introppt2php(finally done)
Chap1introppt2php(finally done)
monikadeshmane
 
Chap1introppt1php basic
Chap1introppt1php basic
monikadeshmane
 
Ad

Recently uploaded (20)

LDMMIA GRAD Student Check-in Orientation Sampler
LDMMIA GRAD Student Check-in Orientation Sampler
LDM & Mia eStudios
 
Unit- 4 Biostatistics & Research Methodology.pdf
Unit- 4 Biostatistics & Research Methodology.pdf
KRUTIKA CHANNE
 
Plate Tectonic Boundaries and Continental Drift Theory
Plate Tectonic Boundaries and Continental Drift Theory
Marie
 
ABCs of Bookkeeping for Nonprofits TechSoup.pdf
ABCs of Bookkeeping for Nonprofits TechSoup.pdf
TechSoup
 
Sustainable Innovation with Immersive Learning
Sustainable Innovation with Immersive Learning
Leonel Morgado
 
june 10 2025 ppt for madden on art science is over.pptx
june 10 2025 ppt for madden on art science is over.pptx
roger malina
 
What is FIle and explanation of text files.pptx
What is FIle and explanation of text files.pptx
Ramakrishna Reddy Bijjam
 
ICT-8-Module-REVISED-K-10-CURRICULUM.pdf
ICT-8-Module-REVISED-K-10-CURRICULUM.pdf
penafloridaarlyn
 
Ray Dalio How Countries go Broke the Big Cycle
Ray Dalio How Countries go Broke the Big Cycle
Dadang Solihin
 
Paper 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
 
What are the benefits that dance brings?
What are the benefits that dance brings?
memi27
 
PEST OF WHEAT SORGHUM BAJRA and MINOR MILLETS.pptx
PEST OF WHEAT SORGHUM BAJRA and MINOR MILLETS.pptx
Arshad Shaikh
 
Exploring Ocean Floor Features for Middle School
Exploring Ocean Floor Features for Middle School
Marie
 
GEOGRAPHY-Study Material [ Class 10th] .pdf
GEOGRAPHY-Study Material [ Class 10th] .pdf
SHERAZ AHMAD LONE
 
BINARY files CSV files JSON files with example.pptx
BINARY files CSV files JSON files with example.pptx
Ramakrishna Reddy Bijjam
 
Wax Moon, Richmond, VA. Terrence McPherson
Wax Moon, Richmond, VA. Terrence McPherson
TerrenceMcPherson1
 
How to Create an Event in Odoo 18 - Odoo 18 Slides
How to Create an Event in Odoo 18 - Odoo 18 Slides
Celine George
 
Introduction to Generative AI and Copilot.pdf
Introduction to Generative AI and Copilot.pdf
TechSoup
 
THERAPEUTIC COMMUNICATION included definition, characteristics, nurse patient...
THERAPEUTIC COMMUNICATION included definition, characteristics, nurse patient...
parmarjuli1412
 
SPENT QUIZ NQL JR FEST 5.0 BY SOURAV.pptx
SPENT QUIZ NQL JR FEST 5.0 BY SOURAV.pptx
Sourav Kr Podder
 
LDMMIA GRAD Student Check-in Orientation Sampler
LDMMIA GRAD Student Check-in Orientation Sampler
LDM & Mia eStudios
 
Unit- 4 Biostatistics & Research Methodology.pdf
Unit- 4 Biostatistics & Research Methodology.pdf
KRUTIKA CHANNE
 
Plate Tectonic Boundaries and Continental Drift Theory
Plate Tectonic Boundaries and Continental Drift Theory
Marie
 
ABCs of Bookkeeping for Nonprofits TechSoup.pdf
ABCs of Bookkeeping for Nonprofits TechSoup.pdf
TechSoup
 
Sustainable Innovation with Immersive Learning
Sustainable Innovation with Immersive Learning
Leonel Morgado
 
june 10 2025 ppt for madden on art science is over.pptx
june 10 2025 ppt for madden on art science is over.pptx
roger malina
 
What is FIle and explanation of text files.pptx
What is FIle and explanation of text files.pptx
Ramakrishna Reddy Bijjam
 
ICT-8-Module-REVISED-K-10-CURRICULUM.pdf
ICT-8-Module-REVISED-K-10-CURRICULUM.pdf
penafloridaarlyn
 
Ray Dalio How Countries go Broke the Big Cycle
Ray Dalio How Countries go Broke the Big Cycle
Dadang Solihin
 
Paper 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
 
What are the benefits that dance brings?
What are the benefits that dance brings?
memi27
 
PEST OF WHEAT SORGHUM BAJRA and MINOR MILLETS.pptx
PEST OF WHEAT SORGHUM BAJRA and MINOR MILLETS.pptx
Arshad Shaikh
 
Exploring Ocean Floor Features for Middle School
Exploring Ocean Floor Features for Middle School
Marie
 
GEOGRAPHY-Study Material [ Class 10th] .pdf
GEOGRAPHY-Study Material [ Class 10th] .pdf
SHERAZ AHMAD LONE
 
BINARY files CSV files JSON files with example.pptx
BINARY files CSV files JSON files with example.pptx
Ramakrishna Reddy Bijjam
 
Wax Moon, Richmond, VA. Terrence McPherson
Wax Moon, Richmond, VA. Terrence McPherson
TerrenceMcPherson1
 
How to Create an Event in Odoo 18 - Odoo 18 Slides
How to Create an Event in Odoo 18 - Odoo 18 Slides
Celine George
 
Introduction to Generative AI and Copilot.pdf
Introduction to Generative AI and Copilot.pdf
TechSoup
 
THERAPEUTIC COMMUNICATION included definition, characteristics, nurse patient...
THERAPEUTIC COMMUNICATION included definition, characteristics, nurse patient...
parmarjuli1412
 
SPENT QUIZ NQL JR FEST 5.0 BY SOURAV.pptx
SPENT QUIZ NQL JR FEST 5.0 BY SOURAV.pptx
Sourav Kr Podder
 

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 = "[email protected]"; $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 = "[email protected]"; $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