简体   繁体   中英

How to capitalize first letter of word in php without ucfirst() function

I am trying to capitalize the first letter of word in php without using ucfirst() function But i am not able do it, but i am struggling with this. Please tell me its answer.

<?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;
?>

We cannot do this without any function. We have to use some function. Like you have applied the for-loop and for strlen function.

<?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;
?>

Without using the function ucfirst , you can do it like this:

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

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

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

To do it for each word, use explode(' ', $string) to get an array of words, or preg_split('#\\s+#', $string, -1, PREG_SPLIT_NO_EMPTY) for better results.

I would advise against just subtracting 32 from the first character of the next word:

  • you do not know it is a letter
  • you do not know it isn't already capitalized
  • you do not know it exists
  • you do not know it is not another space

At the very least check that its ord() value lies between ord('A') and ord('Z') .

To do this all without case-changing functions, you'd do

$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)
    )
);

as such...


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

echo implode(' ', $words);

with ord and 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);

and output

This For Example By Dilo Abininyeri

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM