Regular expressions in PHP allow you to search and manipulate strings based on specific patterns. They are powerful tools for validating input, searching for patterns, and replacing substrings within text.
PHP regular expressions use the PCRE (Perl Compatible Regular Expressions) syntax. Patterns are enclosed within delimiters, often slashes (/pattern/).
The most common functions for regular expressions in PHP are preg_match(), preg_match_all(), preg_replace(), and preg_split().
<?php
$pattern = "/abc/";
$string = "abcdef";
if (preg_match($pattern, $string)) {
echo "Pattern found!";
}
?>
This example demonstrates basic pattern matching using preg_match(). It checks if the string "abcdef" contains the pattern "abc".
Console Output:
Pattern found!
Character classes allow you to match any one of a set of characters. For example, [aeiou] matches any vowel.
Quantifiers specify how many instances of a character or group should be matched. For instance, * matches zero or more times, + matches one or more times.
<?php
$pattern = "/[aeiou]+/";
$string = "hello world";
preg_match_all($pattern, $string, $matches);
print_r($matches);
?>
This example uses preg_match_all() to find all sequences of vowels in the string "hello world".
Console Output:
Array ( [0] => Array ( [0] => e [1] => o [2] => o ) )
You can replace occurrences of a pattern within a string using preg_replace(). This is useful for cleaning up input or formatting text.
<?php
$pattern = "/world/";
$replacement = "PHP";
$string = "hello world";
$new_string = preg_replace($pattern, $replacement, $string);
echo $new_string;
?>
This example uses preg_replace() to replace the word "world" with "PHP" in the string "hello world".
Console Output:
hello PHP
You can make your pattern case-insensitive by adding the i modifier. This allows you to match patterns regardless of case.
<?php
$pattern = "/HELLO/i";
$string = "hello world";
if (preg_match($pattern, $string)) {
echo "Pattern found!";
}
?>
This example demonstrates case-insensitive pattern matching. It finds "hello" in "hello world" regardless of case.
Console Output:
Pattern found!
Anchors allow you to specify the position in the string where a match must occur. ^ matches the start of a string, while $ matches the end.
<?php
$pattern = "/^hello/";
$string = "hello world";
if (preg_match($pattern, $string)) {
echo "String starts with 'hello'.";
}
?>
This example checks if the string "hello world" starts with "hello" using the ^ anchor.
Console Output:
String starts with 'hello'.
Parentheses ( ) are used for grouping and capturing parts of the pattern. This can be useful for extracting specific parts of a string.
<?php
$pattern = "/(hello) (world)/";
$string = "hello world";
preg_match($pattern, $string, $matches);
print_r($matches);
?>
This example demonstrates capturing groups, extracting "hello" and "world" from the string "hello world".
Console Output:
Array ( [0] => hello world [1] => hello [2] => world )
Lookaheads (?=...) and lookbehinds (?<=...) are zero-width assertions that match a group before or after a main pattern without including it in the result.
<?php
$pattern = "/hello(?= world)/";
$string = "hello world";
if (preg_match($pattern, $string)) {
echo "Pattern found with lookahead.";
}
?>
This example demonstrates a positive lookahead, matching "hello" only if it is followed by "world".
Console Output:
Pattern found with lookahead.
The preg_split() function splits a string by a regular expression, returning an array of substrings.
<?php
$pattern = "/[\s,]+/";
$string = "hello, world this is PHP";
$parts = preg_split($pattern, $string);
print_r($parts);
?>
This example uses preg_split() to split the string "hello, world this is PHP" into an array of words.
Console Output:
Array ( [0] => hello [1] => world [2] => this [3] => is [4] => PHP )
Newsletter
Subscribe to our newsletter for weekly updates and promotions.
Wiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWikiCode
Programming LanguagesWeb DevelopmentMobile App DevelopmentData Science and Machine LearningDatabase ManagementDevOps and Cloud ComputingSoftware EngineeringCybersecurityGame DevelopmentWikiCode
Programming LanguagesWeb DevelopmentMobile App DevelopmentData Science and Machine LearningDatabase ManagementDevOps and Cloud ComputingSoftware EngineeringCybersecurityGame DevelopmentWikiCode
Programming LanguagesWeb DevelopmentMobile App DevelopmentData Science and Machine LearningDatabase ManagementDevOps and Cloud ComputingSoftware EngineeringCybersecurityGame DevelopmentWikiCode
Programming LanguagesWeb DevelopmentMobile App DevelopmentData Science and Machine LearningDatabase ManagementDevOps and Cloud ComputingSoftware EngineeringCybersecurityGame DevelopmentWikiCode
Programming LanguagesWeb DevelopmentMobile App DevelopmentData Science and Machine LearningDatabase ManagementDevOps and Cloud ComputingSoftware EngineeringCybersecurityGame DevelopmentWikiCode
Programming LanguagesWeb DevelopmentMobile App DevelopmentData Science and Machine LearningDatabase ManagementDevOps and Cloud ComputingSoftware EngineeringCybersecurityGame DevelopmentWiki News
World NewsPolitics NewsBusiness NewsTechnology NewsHealth NewsScience NewsSports NewsEntertainment NewsEducation NewsWiki News
World NewsPolitics NewsBusiness NewsTechnology NewsHealth NewsScience NewsSports NewsEntertainment NewsEducation NewsWiki News
World NewsPolitics NewsBusiness NewsTechnology NewsHealth NewsScience NewsSports NewsEntertainment NewsEducation NewsWiki News
World NewsPolitics NewsBusiness NewsTechnology NewsHealth NewsScience NewsSports NewsEntertainment NewsEducation NewsWiki News
World NewsPolitics NewsBusiness NewsTechnology NewsHealth NewsScience NewsSports NewsEntertainment NewsEducation NewsWiki News
World NewsPolitics NewsBusiness NewsTechnology NewsHealth NewsScience NewsSports NewsEntertainment NewsEducation NewsWiki Tools
JPEG/PNG Size ReductionPDF Size CompressionPDF Password RemoverSign PDFPower Point to PDFPDF to Power PointJPEG to PDF ConverterPDF to JPEG ConverterWord to PDF ConverterWiki Tools
JPEG/PNG Size ReductionPDF Size CompressionPDF Password RemoverSign PDFPower Point to PDFPDF to Power PointJPEG to PDF ConverterPDF to JPEG ConverterWord to PDF ConverterWiki Tools
JPEG/PNG Size ReductionPDF Size CompressionPDF Password RemoverSign PDFPower Point to PDFPDF to Power PointJPEG to PDF ConverterPDF to JPEG ConverterWord to PDF ConverterWiki Tools
JPEG/PNG Size ReductionPDF Size CompressionPDF Password RemoverSign PDFPower Point to PDFPDF to Power PointJPEG to PDF ConverterPDF to JPEG ConverterWord to PDF ConverterWiki Tools
JPEG/PNG Size ReductionPDF Size CompressionPDF Password RemoverSign PDFPower Point to PDFPDF to Power PointJPEG to PDF ConverterPDF to JPEG ConverterWord to PDF ConverterWiki Tools
JPEG/PNG Size ReductionPDF Size CompressionPDF Password RemoverSign PDFPower Point to PDFPDF to Power PointJPEG to PDF ConverterPDF to JPEG ConverterWord to PDF ConverterCompany
About usCareersPressCompany
About usCareersPressCompany
About usCareersPressLegal
TermsPrivacyContactAds PoliciesLegal
TermsPrivacyContactAds PoliciesLegal
TermsPrivacyContactAds PoliciesCompany
About usCareersPressCompany
About usCareersPressCompany
About usCareersPressLegal
TermsPrivacyContactAds PoliciesLegal
TermsPrivacyContactAds PoliciesLegal
TermsPrivacyContactAds PoliciesLegal
TermsPrivacyContactAds PoliciesAds Policies