SlideShare a Scribd company logo
Pattern Matching
Jigar Makhija
PHP Regular Expression Introduction
• Regular expressions allow you to search for and replace
patterns in strings.
PHP Regular Expression Functions
PHP preg_filter() Function
• Returns a string or an array with pattern matches
replaced, but only if matches were found
<?php
$input = [
"It is 5 o'clock",
"40 days",
"No numbers here",
"In the year 2000"
];
$result = preg_filter('/[0-9]+/', '($0)',
$input);
print_r($result);
?>
O/P:
Array
(
[0] => It is (5) o'clock
[1] => (40) days
[3] => In the year
(2000)
)
PHP Regular Expression Functions
PHP preg_grep() Function
• Returns an array consisting only of elements from the
input array which matched the pattern.
<?php
$input = [
"Red",
"Pink",
"Green",
"Blue",
"Purple"
];
$result = preg_grep("/^p/i", $input);
print_r($result);
?>
O/P:
Array
(
[1] => Pink
[4] => Purple
)
PHP Regular Expression Functions
PHP preg_last_error()
Function
• Returns an error code
indicating the reason that
the most recent regular
expression call failed.
<?php
$str = 'The regular expression is invalid.';
$pattern = '/invalid//';
$match = @preg_match($pattern, $str, $matches);
if($match === false) {
// An error occurred
$err = preg_last_error();
if($err == PREG_INTERNAL_ERROR) {
echo 'Invalid regular expression.';
}
} else if($match) {
// A match was found
echo $matches[0];
} else {
// No matches were found
echo 'No matches found';
}
?>
PHP Regular Expression Functions
PHP preg_match() Function & PHP preg_match_all()
Function
• Finds the first match of a pattern in a string
<?php
$str = "Visit W3Schools";
$pattern = "/w3schools/i";
echo preg_match($pattern, $str);
?>
Finds all matches of a pattern in a string
<?php
$str = "The rain in SPAIN falls mainly on the plains.";
$pattern = "/ain/i";
if(preg_match_all($pattern, $str, $matches)) {
print_r($matches);
}
?>
Array
(
[0] => Array
(
[0] => ain
[1] => AIN
[2] => ain
[3] => ain
)
)
PHP Regular Expression Functions
PHP preg_replace() Function
• Returns a string where matches of a pattern (or an array
of patterns) are replaced with a substring (or an array of
substrings) in a given string
<?php
$str = 'Visit Microsoft!';
$pattern = '/microsoft/i';
echo preg_replace($pattern, 'W3Schools', $str);
?>
O/P:
Visit W3Schools!
PHP Regular Expression Functions
PHP preg_replace_callback() Function
• Given an expression and a callback, returns a string
where all matches of the expression are replaced with the
substring returned by the callback
<?php
function countLetters($matches) {
return $matches[0] . '(' . strlen($matches[0]) . ')';
}
$input = "Welcome to W3Schools.com!";
$pattern = '/[a-z0-9.]+/i';
$result = preg_replace_callback($pattern, 'countLetters',
$input);
echo $result;
?>
O/P:
Welcome(7) to(2) W3Schools.com(13)!
PHP Regular Expression Functions
PHP
preg_replace_callback_array()
Function
• Given an array associating
expressions with callbacks,
returns a string where all
matches of each expression are
replaced with the substring
returned by the callback
<?php
function countLetters($matches) {
return $matches[0] . '[' . strlen($matches[0]) . 'letter]';
}
function countDigits($matches) {
return $matches[0] . '[' . strlen($matches[0]) . 'digit]';
}
$input = "There are 365 days in a year.";
$patterns = [
'/b[a-z]+b/i' => 'countLetters',
'/b[0-9]+b/' => 'countDigits'
];
$result = preg_replace_callback_array($patterns, $inp
echo $result;
?>
O/P:
There[5letter] are[3letter] 365[3digit] days[4letter] in[2letter] a[1letter] year[4letter].
PHP Regular Expression Functions
PHP preg_split() Function
• Breaks a string into an array using matches of a regular
expression as separators
<?php
$date = "1970-01-01 00:00:00";
$pattern = "/[-s:]/";
$components = preg_split($pattern, $date);
print_r($components);
?>
Array
(
[0] => 1970
[1] => 01
[2] => 01
[3] => 00
[4] => 00
[5] => 00
)
PHP Regular Expression Functions
PHP preg_quote() Function
• Escapes characters that have a special meaning in
regular expressions by putting a backslash in front of
them
<?php
$search = preg_quote("://", "/");
$input = 'https://www.w3schools.com/';
$pattern = "/$search/";
if(preg_match($pattern, $input)) {
echo "The input is a URL.";
} else {
echo "The input is not a URL.";
}
?>
O/P:
The input is a URL.
Regular Expression Modifiers
Modifier Description
i Performs a case-insensitive search
m Performs a multiline search (patterns that search for the beginning or end of a string will
match the beginning or end of each line)
u Enables correct matching of UTF-8 encoded patterns
Regular Expression Patterns
Expression Description
[abc] Find one character from the options between the brackets
[^abc] Find any character NOT between the brackets
[0-9] Find one character from the range 0 to 9
Metacharacters
• Metacharacters are characters with a special meaning:
Metacharacter Description
| Find a match for any one of the patterns separated by | as in: cat|dog|fish
. Find just one instance of any character
^ Finds a match as the beginning of a string as in: ^Hello
$ Finds a match at the end of the string as in: World$
d Find a digit
s Find a whitespace character
b Find a match at the beginning of a word like this: bWORD, or at the end of a word like this:
WORDb
uxxxx Find the Unicode character specified by the hexadecimal number xxxx

More Related Content

What's hot

Php introduction
Php introductionPhp introduction
Php introduction
krishnapriya Tadepalli
 
PHP FUNCTIONS
PHP FUNCTIONSPHP FUNCTIONS
PHP FUNCTIONS
Zeeshan Ahmed
 
Jsp ppt
Jsp pptJsp ppt
Jsp ppt
Vikas Jagtap
 
Asp.net state management
Asp.net state managementAsp.net state management
Asp.net state management
priya Nithya
 
File Uploading in PHP
File Uploading in PHPFile Uploading in PHP
File Uploading in PHP
Idrees Hussain
 
Php mysql ppt
Php mysql pptPhp mysql ppt
JavaScript - Chapter 5 - Operators
 JavaScript - Chapter 5 - Operators JavaScript - Chapter 5 - Operators
JavaScript - Chapter 5 - Operators
WebStackAcademy
 
Fundamental JavaScript [UTC, March 2014]
Fundamental JavaScript [UTC, March 2014]Fundamental JavaScript [UTC, March 2014]
Fundamental JavaScript [UTC, March 2014]
Aaron Gustafson
 
Spring JDBCTemplate
Spring JDBCTemplateSpring JDBCTemplate
Spring JDBCTemplate
Guo Albert
 
PHP - Introduction to File Handling with PHP
PHP -  Introduction to  File Handling with PHPPHP -  Introduction to  File Handling with PHP
PHP - Introduction to File Handling with PHP
Vibrant Technologies & Computers
 
Java Server Pages
Java Server PagesJava Server Pages
Java Server Pages
Kasun Madusanke
 
HTML Forms
HTML FormsHTML Forms
HTML Forms
Nisa Soomro
 
JavaScript Tutorial
JavaScript  TutorialJavaScript  Tutorial
JavaScript Tutorial
Bui Kiet
 
Operators in PHP
Operators in PHPOperators in PHP
Operators in PHP
Vineet Kumar Saini
 
Java script
Java scriptJava script
Java script
Abhishek Kesharwani
 
Servlets api overview
Servlets api overviewServlets api overview
Servlets api overview
ramya marichamy
 
Php forms
Php formsPhp forms
Php forms
Anne Lee
 
Document Object Model (DOM)
Document Object Model (DOM)Document Object Model (DOM)
Document Object Model (DOM)
GOPAL BASAK
 
An Introduction to the DOM
An Introduction to the DOMAn Introduction to the DOM
An Introduction to the DOM
Mindy McAdams
 
Php Presentation
Php PresentationPhp Presentation
Php Presentation
Manish Bothra
 

What's hot (20)

Php introduction
Php introductionPhp introduction
Php introduction
 
PHP FUNCTIONS
PHP FUNCTIONSPHP FUNCTIONS
PHP FUNCTIONS
 
Jsp ppt
Jsp pptJsp ppt
Jsp ppt
 
Asp.net state management
Asp.net state managementAsp.net state management
Asp.net state management
 
File Uploading in PHP
File Uploading in PHPFile Uploading in PHP
File Uploading in PHP
 
Php mysql ppt
Php mysql pptPhp mysql ppt
Php mysql ppt
 
JavaScript - Chapter 5 - Operators
 JavaScript - Chapter 5 - Operators JavaScript - Chapter 5 - Operators
JavaScript - Chapter 5 - Operators
 
Fundamental JavaScript [UTC, March 2014]
Fundamental JavaScript [UTC, March 2014]Fundamental JavaScript [UTC, March 2014]
Fundamental JavaScript [UTC, March 2014]
 
Spring JDBCTemplate
Spring JDBCTemplateSpring JDBCTemplate
Spring JDBCTemplate
 
PHP - Introduction to File Handling with PHP
PHP -  Introduction to  File Handling with PHPPHP -  Introduction to  File Handling with PHP
PHP - Introduction to File Handling with PHP
 
Java Server Pages
Java Server PagesJava Server Pages
Java Server Pages
 
HTML Forms
HTML FormsHTML Forms
HTML Forms
 
JavaScript Tutorial
JavaScript  TutorialJavaScript  Tutorial
JavaScript Tutorial
 
Operators in PHP
Operators in PHPOperators in PHP
Operators in PHP
 
Java script
Java scriptJava script
Java script
 
Servlets api overview
Servlets api overviewServlets api overview
Servlets api overview
 
Php forms
Php formsPhp forms
Php forms
 
Document Object Model (DOM)
Document Object Model (DOM)Document Object Model (DOM)
Document Object Model (DOM)
 
An Introduction to the DOM
An Introduction to the DOMAn Introduction to the DOM
An Introduction to the DOM
 
Php Presentation
Php PresentationPhp Presentation
Php Presentation
 

Similar to Php pattern matching

UNIT II (7).pptx
UNIT II (7).pptxUNIT II (7).pptx
UNIT II (7).pptx
DrDhivyaaCRAssistant
 
UNIT II (7).pptx
UNIT II (7).pptxUNIT II (7).pptx
UNIT II (7).pptx
DrDhivyaaCRAssistant
 
String handling and arrays by Dr.C.R.Dhivyaa Kongu Engineering College
String handling and arrays by Dr.C.R.Dhivyaa Kongu Engineering CollegeString handling and arrays by Dr.C.R.Dhivyaa Kongu Engineering College
String handling and arrays by Dr.C.R.Dhivyaa Kongu Engineering College
Dhivyaa C.R
 
Unit 2 - Regular Expression.pptx
Unit 2 - Regular Expression.pptxUnit 2 - Regular Expression.pptx
Unit 2 - Regular Expression.pptx
mythili213835
 
Unit 2 - Regular Expression .pptx
Unit 2 - Regular        Expression .pptxUnit 2 - Regular        Expression .pptx
Unit 2 - Regular Expression .pptx
mythili213835
 
Regular expressions in Python
Regular expressions in PythonRegular expressions in Python
Regular expressions in Python
Sujith Kumar
 
Web app development_php_06
Web app development_php_06Web app development_php_06
Web app development_php_06
Hassen Poreya
 
Regular expressions, Session and Cookies by Dr.C.R.Dhivyaa Kongu Engineering ...
Regular expressions, Session and Cookies by Dr.C.R.Dhivyaa Kongu Engineering ...Regular expressions, Session and Cookies by Dr.C.R.Dhivyaa Kongu Engineering ...
Regular expressions, Session and Cookies by Dr.C.R.Dhivyaa Kongu Engineering ...
Dhivyaa C.R
 
UNIT IV (4).pptx
UNIT IV (4).pptxUNIT IV (4).pptx
UNIT IV (4).pptx
DrDhivyaaCRAssistant
 
PHP Conference Asia 2016
PHP Conference Asia 2016PHP Conference Asia 2016
PHP Conference Asia 2016
Britta Alex
 
Class 5 - PHP Strings
Class 5 - PHP StringsClass 5 - PHP Strings
Class 5 - PHP Strings
Ahmed Swilam
 
unit-4 regular expression.pptx
unit-4 regular expression.pptxunit-4 regular expression.pptx
unit-4 regular expression.pptx
PadreBhoj
 
Regular_Expressions.pptx
Regular_Expressions.pptxRegular_Expressions.pptx
Regular_Expressions.pptx
DurgaNayak4
 
Web Application Development using PHP Chapter 3
Web Application Development using PHP Chapter 3Web Application Development using PHP Chapter 3
Web Application Development using PHP Chapter 3
Mohd Harris Ahmad Jaal
 
WordCamp Portland 2018: PHP for WordPress
WordCamp Portland 2018: PHP for WordPressWordCamp Portland 2018: PHP for WordPress
WordCamp Portland 2018: PHP for WordPress
Alena Holligan
 
PHP Web Programming
PHP Web ProgrammingPHP Web Programming
PHP Web Programming
Muthuselvam RS
 
php_string.pdf
php_string.pdfphp_string.pdf
php_string.pdf
Sharon Manmothe
 
Php basics
Php basicsPhp basics
Php basics
hamfu
 
Manipulating strings
Manipulating stringsManipulating strings
Manipulating strings
Nicole Ryan
 
Regular expressionfunction
Regular expressionfunctionRegular expressionfunction
Regular expressionfunction
ADARSH BHATT
 

Similar to Php pattern matching (20)

UNIT II (7).pptx
UNIT II (7).pptxUNIT II (7).pptx
UNIT II (7).pptx
 
UNIT II (7).pptx
UNIT II (7).pptxUNIT II (7).pptx
UNIT II (7).pptx
 
String handling and arrays by Dr.C.R.Dhivyaa Kongu Engineering College
String handling and arrays by Dr.C.R.Dhivyaa Kongu Engineering CollegeString handling and arrays by Dr.C.R.Dhivyaa Kongu Engineering College
String handling and arrays by Dr.C.R.Dhivyaa Kongu Engineering College
 
Unit 2 - Regular Expression.pptx
Unit 2 - Regular Expression.pptxUnit 2 - Regular Expression.pptx
Unit 2 - Regular Expression.pptx
 
Unit 2 - Regular Expression .pptx
Unit 2 - Regular        Expression .pptxUnit 2 - Regular        Expression .pptx
Unit 2 - Regular Expression .pptx
 
Regular expressions in Python
Regular expressions in PythonRegular expressions in Python
Regular expressions in Python
 
Web app development_php_06
Web app development_php_06Web app development_php_06
Web app development_php_06
 
Regular expressions, Session and Cookies by Dr.C.R.Dhivyaa Kongu Engineering ...
Regular expressions, Session and Cookies by Dr.C.R.Dhivyaa Kongu Engineering ...Regular expressions, Session and Cookies by Dr.C.R.Dhivyaa Kongu Engineering ...
Regular expressions, Session and Cookies by Dr.C.R.Dhivyaa Kongu Engineering ...
 
UNIT IV (4).pptx
UNIT IV (4).pptxUNIT IV (4).pptx
UNIT IV (4).pptx
 
PHP Conference Asia 2016
PHP Conference Asia 2016PHP Conference Asia 2016
PHP Conference Asia 2016
 
Class 5 - PHP Strings
Class 5 - PHP StringsClass 5 - PHP Strings
Class 5 - PHP Strings
 
unit-4 regular expression.pptx
unit-4 regular expression.pptxunit-4 regular expression.pptx
unit-4 regular expression.pptx
 
Regular_Expressions.pptx
Regular_Expressions.pptxRegular_Expressions.pptx
Regular_Expressions.pptx
 
Web Application Development using PHP Chapter 3
Web Application Development using PHP Chapter 3Web Application Development using PHP Chapter 3
Web Application Development using PHP Chapter 3
 
WordCamp Portland 2018: PHP for WordPress
WordCamp Portland 2018: PHP for WordPressWordCamp Portland 2018: PHP for WordPress
WordCamp Portland 2018: PHP for WordPress
 
PHP Web Programming
PHP Web ProgrammingPHP Web Programming
PHP Web Programming
 
php_string.pdf
php_string.pdfphp_string.pdf
php_string.pdf
 
Php basics
Php basicsPhp basics
Php basics
 
Manipulating strings
Manipulating stringsManipulating strings
Manipulating strings
 
Regular expressionfunction
Regular expressionfunctionRegular expressionfunction
Regular expressionfunction
 

More from JIGAR MAKHIJA

Php gd library
Php gd libraryPhp gd library
Php gd library
JIGAR MAKHIJA
 
Db function
Db functionDb function
Db function
JIGAR MAKHIJA
 
C++ version 1
C++  version 1C++  version 1
C++ version 1
JIGAR MAKHIJA
 
C++ Version 2
C++  Version 2C++  Version 2
C++ Version 2
JIGAR MAKHIJA
 
SAP Ui5 content
SAP Ui5 contentSAP Ui5 content
SAP Ui5 content
JIGAR MAKHIJA
 
Solution doc
Solution docSolution doc
Solution doc
JIGAR MAKHIJA
 
Overview on Application protocols in Internet of Things
Overview on Application protocols in Internet of ThingsOverview on Application protocols in Internet of Things
Overview on Application protocols in Internet of Things
JIGAR MAKHIJA
 
125 green iot
125 green iot125 green iot
125 green iot
JIGAR MAKHIJA
 
Msp430 g2 with ble(Bluetooth Low Energy)
Msp430 g2 with ble(Bluetooth Low Energy)Msp430 g2 with ble(Bluetooth Low Energy)
Msp430 g2 with ble(Bluetooth Low Energy)
JIGAR MAKHIJA
 
Embedded system lab work
Embedded system lab workEmbedded system lab work
Embedded system lab work
JIGAR MAKHIJA
 
Presentation on iot- Internet of Things
Presentation on iot- Internet of ThingsPresentation on iot- Internet of Things
Presentation on iot- Internet of Things
JIGAR MAKHIJA
 
Oracle
OracleOracle
Learn Japanese -Basic kanji 120
Learn Japanese -Basic kanji 120Learn Japanese -Basic kanji 120
Learn Japanese -Basic kanji 120
JIGAR MAKHIJA
 
View Alignment Techniques
View Alignment TechniquesView Alignment Techniques
View Alignment Techniques
JIGAR MAKHIJA
 
Letters (complaints & invitations)
Letters (complaints & invitations)Letters (complaints & invitations)
Letters (complaints & invitations)
JIGAR MAKHIJA
 
Letter Writing invitation-letter
Letter Writing invitation-letterLetter Writing invitation-letter
Letter Writing invitation-letter
JIGAR MAKHIJA
 
Communication skills Revised PPT
Communication skills Revised PPTCommunication skills Revised PPT
Communication skills Revised PPT
JIGAR MAKHIJA
 
It tools &amp; technology
It tools &amp; technologyIt tools &amp; technology
It tools &amp; technology
JIGAR MAKHIJA
 
Communication skills
Communication skillsCommunication skills
Communication skills
JIGAR MAKHIJA
 
Use case modeling & analysis v 1
Use case modeling & analysis v 1Use case modeling & analysis v 1
Use case modeling & analysis v 1
JIGAR MAKHIJA
 

More from JIGAR MAKHIJA (20)

Php gd library
Php gd libraryPhp gd library
Php gd library
 
Db function
Db functionDb function
Db function
 
C++ version 1
C++  version 1C++  version 1
C++ version 1
 
C++ Version 2
C++  Version 2C++  Version 2
C++ Version 2
 
SAP Ui5 content
SAP Ui5 contentSAP Ui5 content
SAP Ui5 content
 
Solution doc
Solution docSolution doc
Solution doc
 
Overview on Application protocols in Internet of Things
Overview on Application protocols in Internet of ThingsOverview on Application protocols in Internet of Things
Overview on Application protocols in Internet of Things
 
125 green iot
125 green iot125 green iot
125 green iot
 
Msp430 g2 with ble(Bluetooth Low Energy)
Msp430 g2 with ble(Bluetooth Low Energy)Msp430 g2 with ble(Bluetooth Low Energy)
Msp430 g2 with ble(Bluetooth Low Energy)
 
Embedded system lab work
Embedded system lab workEmbedded system lab work
Embedded system lab work
 
Presentation on iot- Internet of Things
Presentation on iot- Internet of ThingsPresentation on iot- Internet of Things
Presentation on iot- Internet of Things
 
Oracle
OracleOracle
Oracle
 
Learn Japanese -Basic kanji 120
Learn Japanese -Basic kanji 120Learn Japanese -Basic kanji 120
Learn Japanese -Basic kanji 120
 
View Alignment Techniques
View Alignment TechniquesView Alignment Techniques
View Alignment Techniques
 
Letters (complaints & invitations)
Letters (complaints & invitations)Letters (complaints & invitations)
Letters (complaints & invitations)
 
Letter Writing invitation-letter
Letter Writing invitation-letterLetter Writing invitation-letter
Letter Writing invitation-letter
 
Communication skills Revised PPT
Communication skills Revised PPTCommunication skills Revised PPT
Communication skills Revised PPT
 
It tools &amp; technology
It tools &amp; technologyIt tools &amp; technology
It tools &amp; technology
 
Communication skills
Communication skillsCommunication skills
Communication skills
 
Use case modeling & analysis v 1
Use case modeling & analysis v 1Use case modeling & analysis v 1
Use case modeling & analysis v 1
 

Recently uploaded

বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdfবাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
eBook.com.bd (প্রয়োজনীয় বাংলা বই)
 
World environment day ppt For 5 June 2024
World environment day ppt For 5 June 2024World environment day ppt For 5 June 2024
World environment day ppt For 5 June 2024
ak6969907
 
Natural birth techniques - Mrs.Akanksha Trivedi Rama University
Natural birth techniques - Mrs.Akanksha Trivedi Rama UniversityNatural birth techniques - Mrs.Akanksha Trivedi Rama University
Natural birth techniques - Mrs.Akanksha Trivedi Rama University
Akanksha trivedi rama nursing college kanpur.
 
Digital Artifact 1 - 10VCD Environments Unit
Digital Artifact 1 - 10VCD Environments UnitDigital Artifact 1 - 10VCD Environments Unit
Digital Artifact 1 - 10VCD Environments Unit
chanes7
 
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdfANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
Priyankaranawat4
 
Lapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdfLapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdf
Jean Carlos Nunes Paixão
 
Main Java[All of the Base Concepts}.docx
Main Java[All of the Base Concepts}.docxMain Java[All of the Base Concepts}.docx
Main Java[All of the Base Concepts}.docx
adhitya5119
 
Hindi varnamala | hindi alphabet PPT.pdf
Hindi varnamala | hindi alphabet PPT.pdfHindi varnamala | hindi alphabet PPT.pdf
Hindi varnamala | hindi alphabet PPT.pdf
Dr. Mulla Adam Ali
 
ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...
ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...
ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...
PECB
 
The basics of sentences session 6pptx.pptx
The basics of sentences session 6pptx.pptxThe basics of sentences session 6pptx.pptx
The basics of sentences session 6pptx.pptx
heathfieldcps1
 
The Diamonds of 2023-2024 in the IGRA collection
The Diamonds of 2023-2024 in the IGRA collectionThe Diamonds of 2023-2024 in the IGRA collection
The Diamonds of 2023-2024 in the IGRA collection
Israel Genealogy Research Association
 
DRUGS AND ITS classification slide share
DRUGS AND ITS classification slide shareDRUGS AND ITS classification slide share
DRUGS AND ITS classification slide share
taiba qazi
 
Chapter 4 - Islamic Financial Institutions in Malaysia.pptx
Chapter 4 - Islamic Financial Institutions in Malaysia.pptxChapter 4 - Islamic Financial Institutions in Malaysia.pptx
Chapter 4 - Islamic Financial Institutions in Malaysia.pptx
Mohd Adib Abd Muin, Senior Lecturer at Universiti Utara Malaysia
 
A Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in EducationA Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in Education
Peter Windle
 
Executive Directors Chat Leveraging AI for Diversity, Equity, and Inclusion
Executive Directors Chat  Leveraging AI for Diversity, Equity, and InclusionExecutive Directors Chat  Leveraging AI for Diversity, Equity, and Inclusion
Executive Directors Chat Leveraging AI for Diversity, Equity, and Inclusion
TechSoup
 
PCOS corelations and management through Ayurveda.
PCOS corelations and management through Ayurveda.PCOS corelations and management through Ayurveda.
PCOS corelations and management through Ayurveda.
Dr. Shivangi Singh Parihar
 
A Survey of Techniques for Maximizing LLM Performance.pptx
A Survey of Techniques for Maximizing LLM Performance.pptxA Survey of Techniques for Maximizing LLM Performance.pptx
A Survey of Techniques for Maximizing LLM Performance.pptx
thanhdowork
 
Assessment and Planning in Educational technology.pptx
Assessment and Planning in Educational technology.pptxAssessment and Planning in Educational technology.pptx
Assessment and Planning in Educational technology.pptx
Kavitha Krishnan
 
CACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdfCACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdf
camakaiclarkmusic
 
Your Skill Boost Masterclass: Strategies for Effective Upskilling
Your Skill Boost Masterclass: Strategies for Effective UpskillingYour Skill Boost Masterclass: Strategies for Effective Upskilling
Your Skill Boost Masterclass: Strategies for Effective Upskilling
Excellence Foundation for South Sudan
 

Recently uploaded (20)

বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdfবাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
 
World environment day ppt For 5 June 2024
World environment day ppt For 5 June 2024World environment day ppt For 5 June 2024
World environment day ppt For 5 June 2024
 
Natural birth techniques - Mrs.Akanksha Trivedi Rama University
Natural birth techniques - Mrs.Akanksha Trivedi Rama UniversityNatural birth techniques - Mrs.Akanksha Trivedi Rama University
Natural birth techniques - Mrs.Akanksha Trivedi Rama University
 
Digital Artifact 1 - 10VCD Environments Unit
Digital Artifact 1 - 10VCD Environments UnitDigital Artifact 1 - 10VCD Environments Unit
Digital Artifact 1 - 10VCD Environments Unit
 
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdfANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
 
Lapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdfLapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdf
 
Main Java[All of the Base Concepts}.docx
Main Java[All of the Base Concepts}.docxMain Java[All of the Base Concepts}.docx
Main Java[All of the Base Concepts}.docx
 
Hindi varnamala | hindi alphabet PPT.pdf
Hindi varnamala | hindi alphabet PPT.pdfHindi varnamala | hindi alphabet PPT.pdf
Hindi varnamala | hindi alphabet PPT.pdf
 
ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...
ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...
ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...
 
The basics of sentences session 6pptx.pptx
The basics of sentences session 6pptx.pptxThe basics of sentences session 6pptx.pptx
The basics of sentences session 6pptx.pptx
 
The Diamonds of 2023-2024 in the IGRA collection
The Diamonds of 2023-2024 in the IGRA collectionThe Diamonds of 2023-2024 in the IGRA collection
The Diamonds of 2023-2024 in the IGRA collection
 
DRUGS AND ITS classification slide share
DRUGS AND ITS classification slide shareDRUGS AND ITS classification slide share
DRUGS AND ITS classification slide share
 
Chapter 4 - Islamic Financial Institutions in Malaysia.pptx
Chapter 4 - Islamic Financial Institutions in Malaysia.pptxChapter 4 - Islamic Financial Institutions in Malaysia.pptx
Chapter 4 - Islamic Financial Institutions in Malaysia.pptx
 
A Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in EducationA Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in Education
 
Executive Directors Chat Leveraging AI for Diversity, Equity, and Inclusion
Executive Directors Chat  Leveraging AI for Diversity, Equity, and InclusionExecutive Directors Chat  Leveraging AI for Diversity, Equity, and Inclusion
Executive Directors Chat Leveraging AI for Diversity, Equity, and Inclusion
 
PCOS corelations and management through Ayurveda.
PCOS corelations and management through Ayurveda.PCOS corelations and management through Ayurveda.
PCOS corelations and management through Ayurveda.
 
A Survey of Techniques for Maximizing LLM Performance.pptx
A Survey of Techniques for Maximizing LLM Performance.pptxA Survey of Techniques for Maximizing LLM Performance.pptx
A Survey of Techniques for Maximizing LLM Performance.pptx
 
Assessment and Planning in Educational technology.pptx
Assessment and Planning in Educational technology.pptxAssessment and Planning in Educational technology.pptx
Assessment and Planning in Educational technology.pptx
 
CACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdfCACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdf
 
Your Skill Boost Masterclass: Strategies for Effective Upskilling
Your Skill Boost Masterclass: Strategies for Effective UpskillingYour Skill Boost Masterclass: Strategies for Effective Upskilling
Your Skill Boost Masterclass: Strategies for Effective Upskilling
 

Php pattern matching

  • 2. PHP Regular Expression Introduction • Regular expressions allow you to search for and replace patterns in strings.
  • 3. PHP Regular Expression Functions PHP preg_filter() Function • Returns a string or an array with pattern matches replaced, but only if matches were found <?php $input = [ "It is 5 o'clock", "40 days", "No numbers here", "In the year 2000" ]; $result = preg_filter('/[0-9]+/', '($0)', $input); print_r($result); ?> O/P: Array ( [0] => It is (5) o'clock [1] => (40) days [3] => In the year (2000) )
  • 4. PHP Regular Expression Functions PHP preg_grep() Function • Returns an array consisting only of elements from the input array which matched the pattern. <?php $input = [ "Red", "Pink", "Green", "Blue", "Purple" ]; $result = preg_grep("/^p/i", $input); print_r($result); ?> O/P: Array ( [1] => Pink [4] => Purple )
  • 5. PHP Regular Expression Functions PHP preg_last_error() Function • Returns an error code indicating the reason that the most recent regular expression call failed. <?php $str = 'The regular expression is invalid.'; $pattern = '/invalid//'; $match = @preg_match($pattern, $str, $matches); if($match === false) { // An error occurred $err = preg_last_error(); if($err == PREG_INTERNAL_ERROR) { echo 'Invalid regular expression.'; } } else if($match) { // A match was found echo $matches[0]; } else { // No matches were found echo 'No matches found'; } ?>
  • 6. PHP Regular Expression Functions PHP preg_match() Function & PHP preg_match_all() Function • Finds the first match of a pattern in a string <?php $str = "Visit W3Schools"; $pattern = "/w3schools/i"; echo preg_match($pattern, $str); ?> Finds all matches of a pattern in a string <?php $str = "The rain in SPAIN falls mainly on the plains."; $pattern = "/ain/i"; if(preg_match_all($pattern, $str, $matches)) { print_r($matches); } ?> Array ( [0] => Array ( [0] => ain [1] => AIN [2] => ain [3] => ain ) )
  • 7. PHP Regular Expression Functions PHP preg_replace() Function • Returns a string where matches of a pattern (or an array of patterns) are replaced with a substring (or an array of substrings) in a given string <?php $str = 'Visit Microsoft!'; $pattern = '/microsoft/i'; echo preg_replace($pattern, 'W3Schools', $str); ?> O/P: Visit W3Schools!
  • 8. PHP Regular Expression Functions PHP preg_replace_callback() Function • Given an expression and a callback, returns a string where all matches of the expression are replaced with the substring returned by the callback <?php function countLetters($matches) { return $matches[0] . '(' . strlen($matches[0]) . ')'; } $input = "Welcome to W3Schools.com!"; $pattern = '/[a-z0-9.]+/i'; $result = preg_replace_callback($pattern, 'countLetters', $input); echo $result; ?> O/P: Welcome(7) to(2) W3Schools.com(13)!
  • 9. PHP Regular Expression Functions PHP preg_replace_callback_array() Function • Given an array associating expressions with callbacks, returns a string where all matches of each expression are replaced with the substring returned by the callback <?php function countLetters($matches) { return $matches[0] . '[' . strlen($matches[0]) . 'letter]'; } function countDigits($matches) { return $matches[0] . '[' . strlen($matches[0]) . 'digit]'; } $input = "There are 365 days in a year."; $patterns = [ '/b[a-z]+b/i' => 'countLetters', '/b[0-9]+b/' => 'countDigits' ]; $result = preg_replace_callback_array($patterns, $inp echo $result; ?> O/P: There[5letter] are[3letter] 365[3digit] days[4letter] in[2letter] a[1letter] year[4letter].
  • 10. PHP Regular Expression Functions PHP preg_split() Function • Breaks a string into an array using matches of a regular expression as separators <?php $date = "1970-01-01 00:00:00"; $pattern = "/[-s:]/"; $components = preg_split($pattern, $date); print_r($components); ?> Array ( [0] => 1970 [1] => 01 [2] => 01 [3] => 00 [4] => 00 [5] => 00 )
  • 11. PHP Regular Expression Functions PHP preg_quote() Function • Escapes characters that have a special meaning in regular expressions by putting a backslash in front of them <?php $search = preg_quote("://", "/"); $input = 'https://www.w3schools.com/'; $pattern = "/$search/"; if(preg_match($pattern, $input)) { echo "The input is a URL."; } else { echo "The input is not a URL."; } ?> O/P: The input is a URL.
  • 12. Regular Expression Modifiers Modifier Description i Performs a case-insensitive search m Performs a multiline search (patterns that search for the beginning or end of a string will match the beginning or end of each line) u Enables correct matching of UTF-8 encoded patterns Regular Expression Patterns Expression Description [abc] Find one character from the options between the brackets [^abc] Find any character NOT between the brackets [0-9] Find one character from the range 0 to 9
  • 13. Metacharacters • Metacharacters are characters with a special meaning: Metacharacter Description | Find a match for any one of the patterns separated by | as in: cat|dog|fish . Find just one instance of any character ^ Finds a match as the beginning of a string as in: ^Hello $ Finds a match at the end of the string as in: World$ d Find a digit s Find a whitespace character b Find a match at the beginning of a word like this: bWORD, or at the end of a word like this: WORDb uxxxx Find the Unicode character specified by the hexadecimal number xxxx