-4

As i am using PHP, So problem comes to me that how can i captilized the first Letter In this Code.

<?php foreach($aa as $row):?>

<?php echo ''.$row->username.'' ?>
<?php endforeach; ?>
Shahnewaz
  • 360
  • 4
  • 12
user2246395
  • 31
  • 1
  • 2
  • Have you googled this at least? there's a function called `ucfirst()` – slash197 Apr 23 '13 at 09:17
  • perhaps using the ucfirst() function? http://www.php.net/manual/en/function.ucfirst.php and reading the PHP docs – Mark Baker Apr 23 '13 at 09:17
  • There are so many questions here already concerning this problem: http://stackoverflow.com/questions/5536386/how-to-make-first-letter-of-a-word-capital?rq=1 and http://stackoverflow.com/questions/4625954/capitalize-first-letter-of-each-word-pulled-from-mysql-with-php-jquery?rq=1 and ... (look in the list to the right). – Till Helge Apr 23 '13 at 09:17
  • So is it letter or word? – Artjom Kurapov Apr 23 '13 at 09:18
  • [ucfirst](http://php.net/manual/en/function.ucfirst.php). Google doesnt work where you live? – Bojan Kovacevic Apr 23 '13 at 09:18

4 Answers4

6

Php has a ucfirst() function it Make a string's first character uppercase.

<?php echo ''.ucfirst($row->username).'' ?>

NullPoiиteя
  • 56,591
  • 22
  • 125
  • 143
MatthewMcGovern
  • 3,466
  • 1
  • 19
  • 19
1

ucfirst() will help you, but note that it will only convert the first to upper case. All others can also be in upper case, so you may want those to be converted to lower case first.

<?php echo ucfirst(strtolower($row->username)); ?>
Spontifixus
  • 6,570
  • 9
  • 45
  • 63
user410932
  • 2,915
  • 4
  • 22
  • 23
0

it'd be easier for you to use CSS instead of a PHP function to display this.

use this line of CSS:

text-transform: capitalize;
Savv
  • 433
  • 2
  • 7
0

Safe UTF-8 method

<?php echo asd($row->firstname); ?>

    function asd($string){
     if(mb_strlen($string)){
     return mb_strtoupper(mb_substr($string,0,1)).mb_substr($string,1,mb_strlen($string));
    }else{
     return false;
    }
    }

but in Codeigniter you can just do:

$this->load->helper('string');
echo humanize($row->username);

Third case (the one i prefer usually) is to use CSS class:

.capitalize{
 text-transform:capitalize;
}

<a class="capitalize"><?php echo $row->firstname; ?></a>
itsme
  • 48,972
  • 96
  • 224
  • 345