PHP Regular Expressions
In this page:
preg_match("/pattern/", $subject, $matches); // first match
preg_match_all("/pattern/", $subject, $matches); // every match
$result = preg_replace("/pattern/", "replacement", $subject);
Basic Match Checking
Regular expressions special metacharacters इस्तेमाल करके एक text pattern describe करते हैं, आपको exact literal value के बजाय एक shape follow करने वाली strings match, extract, या validate करने देते हुए।
PHP के preg_* functions PCRE (Perl-Compatible Regular Expressions) engine को wrap करते हैं, इसलिए वही pattern syntax जो आप Perl या कई दूसरी languages में इस्तेमाल करेंगे यहाँ भी काम करता है।
उदाहरण: Basic Match Checking
<?php
// Declare `$pattern`, set to "/^[a-z]+$/"
$pattern = "/^[a-z]+$/";
// Print a detailed dump (with types) of `preg_match($pattern, "hello")`
var_dump(preg_match($pattern, "hello"));
?>
Login to try C/C++/Java/PHP code in the editor
सभी Matches ढूँढना
preg_match() किसी string में किसी pattern की पहली occurrence search करता है और report करता है कि यह मिली या नहीं, optionally matched text और किसी captured groups के साथ एक output array भरते हुए।
यह एक email address या phone number roughly किसी expected shape से match करता है इसे confirm करने जैसे validation checks का workhorse है।
उदाहरण: Finding All Matches
<?php
// Declare `$email`, set to "[email protected]"
$email = "[email protected]";
// Check whether `preg_match("/^\S+@\S+\.\S+$/", $email)`
if (preg_match("/^\S+@\S+\.\S+$/", $email)) {
// Print "Valid email format" to the output
echo "Valid email format";
}
?>
Login to try C/C++/Java/PHP code in the editor
Patterns के साथ Text Replace करना
preg_match_all() preg_match() जैसा behave करता है लेकिन पहले hit के बाद भी scan करता रहता है, पहले पर रुकने के बजाय string में हर match return करते हुए।
इसे तब इस्तेमाल करें जब आपको किसी pattern की सभी occurrences निकालनी हों, जैसे text के किसी block में हर hashtag या URL।
उदाहरण: Replacing Text with Patterns
<?php
// Declare `$text`, set to "Contact #alice and #bob for details"
$text = "Contact #alice and #bob for details";
// Call `preg_match_all("/#(\w+)/", $text, $matches)`
preg_match_all("/#(\w+)/", $text, $matches);
// Print a human-readable dump of `$matches[1]`
print_r($matches[1]);
?>
Login to try C/C++/Java/PHP code in the editor
Pattern से String Split करना
preg_replace() किसी pattern का हर match ढूँढता है और इसे replacement text से swap कर देता है, backreferences support करते हुए ताकि आप replacement के अंदर captured groups reuse कर सकें।
यह इसे data reformat करने के लिए उपयोगी बनाता है, जैसे 'lastname, firstname' को एक call में 'firstname lastname' में बदलना।
उदाहरण: Splitting String by Pattern
<?php
// Declare `$name`, set to "Doe, Jane"
$name = "Doe, Jane";
// Declare `$result`, set to `preg_replace("/(\w+), (\w+)/", "$2 $1", $name)`
$result = preg_replace("/(\w+), (\w+)/", "$2 $1", $name);
// Print `$result` to the output
echo $result;
?>
Login to try C/C++/Java/PHP code in the editor
Pattern Validation
Delimiters (आमतौर पर forward slashes) एक pattern को wrap करते हैं ताकि PHP को पता चले कि यह कहाँ शुरू और खत्म होता है, और closing delimiter के बाद रखे गए modifier flags matching behavior बदलते हैं, जैसे case-insensitive matching के लिए i या हर line को ^ और $ से अलग treat करने के लिए m।
उदाहरण: Pattern Validation
<?php
// Declare `$text`, set to "Hello World"
$text = "Hello World";
// Print a detailed dump (with types) of `preg_match("/hello/i", $text)`
var_dump(preg_match("/hello/i", $text));
?>
Login to try C/C++/Java/PHP code in the editor
- delimiters भूल जाना, जैसे
preg_match(abc, $s), जो एक warning produce करता है क्योंकि किसी pattern को/.../चाहिए। - यह भूल जाना कि
preg_match()matches का एक array नहीं,1,0याfalsereturn करता है, इसलिए उन्हें पाने के लिए तीसरा argument इस्तेमाल करें। - pattern में
.जैसे special characters escape न करना, ताकि/a.b/axbसे भी match करे।
Chapter Quiz — Complete all 8 topics to unlock
0/8 topics done
Complete these topics first: