Use the s
modifier (also called DOTALL
modifier). The dot metacharacter, by default, matches everything except newlines. The s
modifier makes it match newlines as well:
$newstr = preg_replace_callback("~<p>(.+?)</p>~s", function($matches) {
return str_replace($matches[1], '<b>' . $matches[1] . '</b>', $matches[0]);
}, $str);