PHP 用大写替换每行的第一个小写字符

PHP Replace the very first lower cased char of every line by upper case

我试过使用下面的

$string= 'hello world<br>this is newline<br><br>another line';
echo $string.'<hr><br>';
echo preg_replace_callback('/^[a-zA-Z0-9]/m', function($m) {
  return strtoupper($m[0]);
}, $string);

输出:

Hello world
this is newline

another line

我也需要更改单词 "this" 和 "another" 的首字母。所以输出变成这样:

Hello world
This is newline

Another line

谢谢。

您可以通过以下方式实现您的目标:

$string= 'hello world<br>this is newline<br><br>another line<hr><br>';

$parts = preg_split( "/<br>|<hr>/", $string ); // split string by either <br> or <hr>
$parts = array_filter($parts); // clean array from empty values
$parts = array_map('ucfirst',$parts); // array of lines with first letter capitalized

echo $partString = implode("<br>",$parts);