पीएचपी preg_match_all () समारोह
उदाहरण
एक स्ट्रिंग में "ऐन" की सभी घटनाओं का पता लगाएं:
<?php
$str = "The rain in SPAIN falls mainly on the plains.";
$pattern =
"/ain/i";
if(preg_match_all($pattern, $str, $matches)) {
print_r($matches);
}
?>
परिभाषा और उपयोग
preg_match_all()
फ़ंक्शन एक स्ट्रिंग में पाए गए पैटर्न के मैचों की संख्या देता है और पाए गए मैचों के साथ एक चर को पॉप्युलेट करता है ।
वाक्य - विन्यास
preg_match_all(pattern, input, matches, flags, offset)
पैरामीटर मान
Parameter | Description |
---|---|
pattern | Required. Contains a regular expression indicating what to search for |
input | Required. The string in which the search will be performed |
matches | Optional. The variable used in this parameter will be populated with an array containing all of the matches that were found |
flags | Optional. A set of options that change how the matches array is structured. One of the following structures may be selected:
|
offset | Optional. Defaults to 0. Indicates how far into the string to begin searching. The preg_match() function will not find matches that occur before the position given in this parameter |
टेक्निकल डिटेल
प्रतिलाभ की मात्रा: | यदि कोई त्रुटि हुई तो पाए गए या गलत होने वाले मैचों की संख्या लौटाता है |
---|---|
पीएचपी संस्करण: | 4+ |
चेंजलॉग: | PHP 7.2 - PREG_UNMATCHED_AS_NULL ध्वज PHP 5.4 जोड़ा गया - मिलान पैरामीटर वैकल्पिक हो गया PHP 5.3.6 - ऑफ़सेट इनपुट की लंबाई से अधिक लंबा होने पर फ़ंक्शन झूठा लौटाता है PHP 5.2.2 - नामित उप-पैटर्न (?'name') का उपयोग कर सकते हैं ) और (? <name>) सिंटैक्स पिछले (?P<name>) के अलावा |
और ज्यादा उदाहरण
उदाहरण
मिलान सरणी की संरचना सेट करने के लिए PREG_PATTERN_ORDER का उपयोग करें । इस उदाहरण में, मिलान सरणी के प्रत्येक तत्व में रेगुलर एक्सप्रेशन के समूहों में से किसी एक के लिए सभी मिलान होते हैं।
<?php
$str = "abc ABC";
$pattern = "/((a)b)(c)/i";
if(preg_match_all($pattern,
$str, $matches, PREG_PATTERN_ORDER)) {
print_r($matches);
}
?>
❮ PHP RegExp संदर्भ