Note: The following is intended to be a general answer and does not attempt to solve the OP's specific problem as it has already been addressed before.
What is preg_replace_callback()
?
This function is used to perform a regular expression search-and-replace. It is similar to str_replace()
, but instead of plain strings, it searches for a user-defined regex pattern, and then applies the callback function on the matched items. The function returns the modified string if matches are found, unmodified string otherwise.
When should I use it?
preg_replace_callback()
is very similar to preg_replace()
- the only difference is that instead of specifying a replacement string for the second parameter, you specify a callback
function.
Use preg_replace()
when you want to do a simple regex search and replace. Use preg_replace_callback()
when you want to do more than just replace. See the example below for understanding how it works.
How to use it?
Here's an example to illustrate the usage of the function. Here, we are trying to convert a date string from YYYY-MM-DD
format to DD-MM-YYYY
.
// our date string
$string = '2014-02-22';
// search pattern
$pattern = '~(\d{4})-(\d{2})-(\d{2})~';
// the function call
$result = preg_replace_callback($pattern, 'callback', $string);
// the callback function
function callback ($matches) {
print_r($matches);
return $matches[3].'-'.$matches[2].'-'.$matches[1];
}
echo $result;
Here, our regular expression pattern searches for a date string of the format NNNN-NN-NN
where N
could be a digit ranging from 0
- 9
(\d
is a shorthand representation for the character class [0-9]
). The callback function will be called and passed an array of matched elements in the given string.
The final result will be:
22-02-2014
Note: The above example is for illustration purposes only. You should not use to parse dates. Use DateTime::createFromFormat()
and DateTime::format()
instead. This question has more details.
$in
populated with subject ? – Cao