PHP preg_replace_callback_array () फ़ंक्शन
उदाहरण
दिखाएँ कि एक वाक्य में प्रत्येक शब्द में कितने अक्षर या अंक पाए जाते हैं:
<?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, $input);
echo $result;
?>
परिभाषा और उपयोग
preg_replace_callback_array()
फ़ंक्शन एक स्ट्रिंग या स्ट्रिंग्स की एक सरणी देता है जिसमें नियमित अभिव्यक्तियों के एक सेट के मिलान को कॉलबैक फ़ंक्शन के रिटर्न मान से बदल दिया जाता है ।
नोट: प्रत्येक स्ट्रिंग के लिए, फ़ंक्शन दिए गए क्रम में पैटर्न का मूल्यांकन करता है। स्ट्रिंग पर पहले पैटर्न का मूल्यांकन करने का परिणाम दूसरे पैटर्न के लिए इनपुट स्ट्रिंग के रूप में उपयोग किया जाता है और इसी तरह। इससे अप्रत्याशित व्यवहार हो सकता है।
वाक्य - विन्यास
preg_replace_callback_array(patterns, input, limit, count)
पैरामीटर मान
Parameter | Description |
---|---|
pattern | Required. An associative array which associates regular
expression patterns to callback functions. The callback functions have one parameter which is an array of matches.The first element in the array contains the match for the whole expression while the remaining elements have matches for each of the groups in the expression. |
input | Required. The string or array of strings in which replacements are being performed |
limit | Optional. Defaults to -1, meaning unlimited. Sets a limit to how many replacements can be done in each string |
count | Optional. After the function has executed, this variable will contain a number indicating how many replacements were performed |
टेक्निकल डिटेल
प्रतिलाभ की मात्रा: | इनपुट स्ट्रिंग या स्ट्रिंग्स में प्रतिस्थापन लागू करने के परिणामस्वरूप एक स्ट्रिंग या स्ट्रिंग की एक सरणी देता है |
---|---|
पीएचपी संस्करण: | 7+ |
और ज्यादा उदाहरण
उदाहरण
यह उदाहरण क्रम में मूल्यांकन किए जा रहे पैटर्न के संभावित अप्रत्याशित प्रभावों को दिखाता है। सबसे पहले, काउंटलेटर्स प्रतिस्थापन "[4letter]" को "दिनों" में जोड़ता है और, उसके प्रतिस्थापन के बाद, काउंटडिजिट्स प्रतिस्थापन "4" को "4letter" में ढूंढता है और उसमें "[1digit]" जोड़ता है:
<?php
function countLetters($matches) {
return $matches[0] . '[' .
strlen($matches[0]) . 'letter]';
}
function countDigits($matches) {
return $matches[0] . '[' . strlen($matches[0]) . 'digit]';
}
$input
= "365 days";
$patterns = [
'/[a-z]+/i' => 'countLetters',
'/[0-9]+/' => 'countDigits'
];
$result =
preg_replace_callback_array($patterns, $input);
echo $result;
?>
❮ PHP RegExp संदर्भ