PHP preg_replace_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;
?>
परिभाषा और उपयोग
preg_replace_callback()
फ़ंक्शन, एक अभिव्यक्ति और कॉलबैक दिया गया है, एक स्ट्रिंग देता है जहां अभिव्यक्ति के सभी मैचों को कॉलबैक फ़ंक्शन द्वारा लौटाए गए सबस्ट्रिंग के साथ बदल दिया जाता है ।
वाक्य - विन्यास
preg_replace_callback(pattern, callback, input, limit, count)
पैरामीटर मान
Parameter | Description |
---|---|
pattern | Required. A regular expression or array of regular expressions indicating what to search for |
replacements | Required. A callback function which returns the replacement. The callback function has one parameter containing 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 |
टेक्निकल डिटेल
प्रतिलाभ की मात्रा: | इनपुट स्ट्रिंग या स्ट्रिंग्स में प्रतिस्थापन लागू करने के परिणामस्वरूप एक स्ट्रिंग या स्ट्रिंग की एक सरणी देता है। |
---|---|
पीएचपी संस्करण: | 4.0.5+ |
चेंजलॉग: | PHP 5.1.0 - गणना पैरामीटर जोड़ा गया था |
❮ PHP RegExp संदर्भ