Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make the first 2 characters of a string uppercase?

Tags:

string

php

How can I use the PHP strtoupper function for the first two characters of a string? Or is there another function for that?

So the string 'hello' or 'Hello' must be converted to 'HEllo'.

like image 433
Jordy Avatar asked Aug 10 '11 15:08

Jordy


People also ask

How do you create a character in a string uppercase?

toUpperCase(char ch) converts the character argument to uppercase using case mapping information from the UnicodeData file.

How do you convert the first letter of string to uppercase in TypeScript?

To capitalize the first letter of a string in TypeScript: Use the charAt() method to get the first letter of the string. Call the toUpperCase() method on the letter. Use the slice() method to get the rest of the string. Concatenate the results.

How do you capitalize the first letter in a string Javascript?

To capitalize the first character of a string, We can use the charAt() to separate the first character and then use the toUpperCase() function to capitalize it.

How do you check if the first letter of a string is capital?

To check if the first letter of a string is uppercase, call the toUppercase() method to convert the first letter to uppercase and compare it to itself. If the comparison returns true , then the first letter is uppercase.


1 Answers

$txt = strtoupper( substr( $txt, 0, 2 ) ).substr( $txt, 2 );

This works also for strings that are less than 2 characters long.

like image 184
JJJ Avatar answered Sep 29 '22 20:09

JJJ