- preg_replace_callback: documentation ( source)
<?php
function countLetters($matches) {
return $matches[0] . '(' . strlen($matches[0]) . ')';
}
$input = "Welcome to W3Schools.com!";
$pattern = '/[a-z0-9\.]+/i';
$limit = -1;
$count = null;
$flag = 0;
$result = preg_replace_callback($pattern, 'countLetters', $input, $limit, $count, $flag);
echo $result;