【问题标题】:Making the first character of every sentence capital case [duplicate]使每个句子的第一个字符大写[重复]
【发布时间】:2016-03-12 07:03:03
【问题描述】:

我有一个函数可以使每个新句子的字符都大写,但是它不能正常工作。仅当新单词与标点符号正对时才有效,而标点符号后有空格则无效。我该如何解决这个问题?

//****************************************************************
function ucAll($str) {

return preg_replace_callback('/(?<=^|[\.\?!])[^\.]/', function ($match) {
return strtoupper($match[0]);
}, $str);

} //end of function ucAll($str)
//****************************************************************

$string = "i dont' want to? why should i?";
$string = ucAll($string);
echo $string;

结果

我不想?我为什么要这样做?

需要结果

我不想?我为什么要这样做?

【问题讨论】:

  • @rizier123 /e 修饰符已弃用,请改用 preg_replace_callback

标签: php preg-replace uppercase


【解决方案1】:

只需在正则表达式的适当位置添加(\s)*

<?php

//****************************************************************
function ucAll($str) {

    return preg_replace_callback('/(?<=^|[\.\?!])(\s)*[^\.]/', function ($match) {
        return strtoupper($match[0]);
    }, $str);

} //end of function ucAll($str)
//****************************************************************

$string = "i dont' want to? why should i?";
$string = ucAll($string);
echo $string;

【讨论】:

    猜你喜欢
    • 2019-09-04
    • 1970-01-01
    • 2014-08-17
    • 1970-01-01
    • 1970-01-01
    • 2014-05-13
    • 1970-01-01
    • 2018-03-06
    • 1970-01-01
    相关资源
    最近更新 更多