PHP将每行的第一个小写字符替换为大写字符
我试过使用下面的PHP将每行的第一个小写字符替换为大写字符,php,regex,preg-replace,preg-replace-callback,Php,Regex,Preg Replace,Preg Replace Callback,我试过使用下面的 $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); 我还需要更改单词“this”和“othe
$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);
我还需要更改单词“this”和“other”的第一个字母。因此,输出如下所示:
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);
$string='helloworld
这是新行
另一行
';
$parts=preg_split(“/
/”,$string);//通过
或
拆分字符串
$parts=array_filter($parts);//从空值中清除数组
$parts=数组_映射('ucfirst',$parts);//首字母大写的行数组
echo$partString=内爆(“
”,$parts);
除了呈现HTML的浏览器/应用程序之外,在任何方面都不是新行。您可以尝试/(^ |
)
或将它们转换为新行。另外,
可能比字符类更容易,除非您只想大写a-z
(如果是这种情况,字符类可以缩减为[a-z]
)。为什么要包括a-z
和0-9
?大写字母是什么?您可以将正则表达式更改为(请参见正则表达式101的说明)。这是一个很好的例子。很高兴你来这里
$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);