Php 仅当第一个字母为';这不是大写字母

Php 仅当第一个字母为';这不是大写字母,php,Php,所以我必须制作一个网页,其中包含一个可以填写的文本区域(用空格分隔的单词) 因此,文本中的每个单词(每行一个单词)都必须显示在屏幕上,其中每个单词的大写字母都转换为小写字母,除非正在处理的单词的第一个字母是大写字母 示例:''这是StacKOverFlOW站点''将是''这是StacKOverFlOW站点' 我知道我必须处理explode()、strotoupper()和strotlower(),我就是无法让代码正常工作 $text = 'tHIs is the StacKOverFlOW Si

所以我必须制作一个网页,其中包含一个可以填写的文本区域(用空格分隔的单词)

因此,文本中的每个单词(每行一个单词)都必须显示在屏幕上,其中每个单词的大写字母都转换为小写字母,除非正在处理的单词的第一个字母是大写字母

示例:''这是StacKOverFlOW站点''将是''这是StacKOverFlOW站点'

我知道我必须处理explode()、strotoupper()和strotlower(),我就是无法让代码正常工作

$text = 'tHIs is the StacKOverFlOW SiTE';
$oldWords = explode(' ', $text);
$newWords = array();

foreach ($oldWords as $word) {
    if ($word[0] == strtoupper($word[0])
        $word = ucfirst(strtolower($word));
    else
        $word = strtolower($word);

    $newWords[] = $word;
}
更新:

以下是处理其他一些情况的更好版本:

$sentence = "Is tHIs, the StacKOverFlOW SiTE?\n(I doN'T know) [A.C.R.O.N.Y.M] 3AM";
$new_sentence = preg_replace_callback(
    "/(?<=\b\w)(['\w]+)/",
    function($matches) { return strtolower($matches[1]); },
    $sentence);
echo $new_sentence; 
// Is this, the Stackoverflow Site?
// (I don't know) [A.C.R.O.N.Y.M] 3am
// OUTPUT OF OLD VERSION:
// Is this, the Stackoverflow Site?
// (i don't know) [a.c.r.o.n.y.m] 3am
这是StacKOverFlOW站点吗?\n(我不知道)[A.C.R.O.n.Y.M]凌晨3点”; $new\u session=preg\u replace\u回调( "/(? 更新:

以下是处理其他一些情况的更好版本:

$sentence = "Is tHIs, the StacKOverFlOW SiTE?\n(I doN'T know) [A.C.R.O.N.Y.M] 3AM";
$new_sentence = preg_replace_callback(
    "/(?<=\b\w)(['\w]+)/",
    function($matches) { return strtolower($matches[1]); },
    $sentence);
echo $new_sentence; 
// Is this, the Stackoverflow Site?
// (I don't know) [A.C.R.O.N.Y.M] 3am
// OUTPUT OF OLD VERSION:
// Is this, the Stackoverflow Site?
// (i don't know) [a.c.r.o.n.y.m] 3am
这是StacKOverFlOW站点吗?\n(我不知道)[A.C.R.O.n.Y.M]凌晨3点”; $new\u session=preg\u replace\u回调( “/(?我会这样写

$tabtext=explode(' ',$yourtext);
foreach($tabtext as $k=>$v)
{
    $tabtext[$k]=substr($v,0,1).strtolower(substr($v,1));
}
$yourtext=implode(' ',$tabtext);
我会这样写的

$tabtext=explode(' ',$yourtext);
foreach($tabtext as $k=>$v)
{
    $tabtext[$k]=substr($v,0,1).strtolower(substr($v,1));
}
$yourtext=implode(' ',$tabtext);

ucfirst(strtolower($your_text))
会让你接近。但是如果第一个字母不是大写,那么你只需要大写字母,在做ucfirst/strtolower之前,你需要进行分解和一些测试。这将构成所有单词的第一个字母。请与我们分享你到目前为止的代码;这样我们就可以知道问题。
ucfirst(strtolower($your_text))
会让你接近。但是,如果第一个字母不是大写,你只需要大写字母,在做ucfirst/strtolower之前,你需要进行分解和一些测试。这将使所有单词的第一个字母都向上。请与我们分享你到目前为止的代码;这样我们可能会发现问题。