【问题标题】:How to capitalize first letter of word in php without ucfirst() function如何在没有ucfirst()函数的情况下将php中单词的首字母大写
【发布时间】:2020-12-05 18:33:30
【问题描述】:

我试图在不使用 ucfirst() 函数的情况下将 php 中单词的第一个字母大写但我做不到,但我正在为此苦苦挣扎。请告诉我它的答案。

<?php

   $str ="the resources of earth make life possible on it";
   $str[0] = chr(ord($str[0])-32);  
   $length = strlen($str);
  
   for($pos=0; $pos<$length; $pos++){
           if($str[$pos]==' '){
               $str[$pos+1] = chr(ord($str[$pos+1])-32);
       }
    }
   echo $str;
?>

【问题讨论】:

  • chr(ord($str[0]-32));,你的右括号放错地方了,试试chr(ord($str[0])-32)
  • 您应该明确哪些功能不能使用。 chr() 和 ord() 是函数。

标签: php for-loop if-statement


【解决方案1】:

不使用函数ucfirst,你可以这样做:

$firstLetter = substr($word, 0, 1);
$restOfWord  = substr($word, 1);

$firstLetter = strtoupper($firstLetter);
$restOfWord  = strtolower($restOfWord);

print "{$firstLetter}{$restOfWord}\n";

要对每个单词执行此操作,请使用 explode(' ', $string) 获取单词数组,或使用 preg_split('#\\s+#', $string, -1, PREG_SPLIT_NO_EMPTY) 以获得更好的结果。

我不建议只从下一个单词的第一个字符中减去 32:

  • 你不知道这是一封信
  • 你不知道它还没有大写
  • 你不知道它存在
  • 你不知道它不是另一个空间

至少检查其ord() 值是否介于ord('A')ord('Z') 之间。

要在不使用大小写更改功能的情况下完成这一切,您应该这样做

$text = implode(' ',
    array_map(
        function($word) {
            $firstLetter = substr($word, 0, 1);
            if ($firstLetter >= 'a' && $firstLetter <= 'z') {
                $firstLetter = chr(ord($firstLetter)-32);
            }
            $restOfWord  = substr($word, 1);
            $len = strlen($restOfWord);
            for ($i = 0; $i < $len; $i++) {
                if ($restOfWord[$i] >= 'A' && $restOfWord[$i] <= 'Z') {
                    $restOfWord[$i] = chr(ord(restOfWord[$i])+32);
                }
            }
            return $firstLetter . $restOfWord;
        },
        preg_split('#\\s+#', $originalText, -1, PREG_SPLIT_NO_EMPTY)
    )
);

【讨论】:

  • 我没有投反对票,但我很确定问题是如何在没有任何功能或至少任何大小写更改功能的情况下做到这一点。
  • @gview 你可能是对的。在答案中添加了功能不佳的情况。
【解决方案2】:

这样……


$str ="the resources of earth make life possible on it";
$words=array_map(static fn($a) => ucfirst($a), explode(' ', $str));

echo implode(' ', $words);

使用 ord 和 chr


$ordb=ord('b'); //98

$capitalB=chr(98-32); //B

$ordA=ord('a'); //97

$caiptalA=chr(97-32); //A

//so 




function capitalize(string $word)
{

$newWord = '';

$previousCharIsEmpty = true;
$length = strlen($word);

for ($a = 0; $a < $length; $a++) {
    if ($word[$a] === ' ') {
        $newWord .= ' ';
        $previousCharIsEmpty = true;
    } else {
        if ($previousCharIsEmpty === true) {
            $ord = ord($word[$a]);
            $char = chr($ord - 32);
            $newWord .= $char;
            $previousCharIsEmpty = false;
        } else {
            $newWord .= $word[$a];
        }
        $previousCharIsEmpty = false;
    }

return $newWord;
   
}

$word = 'this for example by dilo abininyeri';
echo capitalize($word);

和输出

This For Example By Dilo Abininyeri

【讨论】:

  • 问题询问如何在没有 php 函数的情况下执行此操作。
  • 明天我将发送另一个没有 PHP 函数的示例
  • 代码非常多
【解决方案3】:

没有任何功能我们无法做到这一点。我们必须使用一些功能。就像您应用了 for-loop 和 for strlen 函数一样。

<?php

   $str ="the resources of earth make life possible on it";
   $str[0] = chr(ord($str[0])-32);  
   $length = strlen($str);
  
   for($pos=0; $pos<$length; $pos++){
           if($str[$pos]==' '){
               $str[$pos+1] = chr(ord($str[$pos+1])-32);
       }
    }
   echo $str;
?>

【讨论】:

    猜你喜欢
    • 2015-02-02
    • 1970-01-01
    • 2016-05-01
    • 2011-07-07
    • 1970-01-01
    • 2015-11-10
    • 1970-01-01
    • 2016-02-15
    • 2018-03-28
    相关资源
    最近更新 更多