Often in a hurry when adding materials to a site or, for example, creating new topic on the forum, the user can start writing a sentence (title) with a small (lowercase) letter. This is to some extent a mistake.

I will show several options for solving this problem: PHP and CSS are more suitable for already published materials, when jQuery can correct the situation before publication.

The first letter of the string in uppercase in PHP

In PHP there is a function called “ucfirst”, which just converts the first character of a line to uppercase, but its disadvantage is that it does not work quite correctly with Cyrillic.

To do this, we will write our own small function. The implementation would look like this:

In this version, we will receive a sentence that begins with a capital letter, which, in fact, is what we need.

Uppercase first letter of a string in CSS

This method is visual (that is, in source code site proposals will appear as is) also converts the first character to uppercase.

The usage is as follows:

first sentence

second sentence

third sentence

fourth sentence

#content p:first-letter ( text-transform: uppercase; )

Using the “first-letter” pseudo-element and the “text-transform” property, we set the design for each first letter of the paragraph.

First letter of a string in uppercase in jQuery

As I said earlier, this conversion method is best suited for materials that are yet to be published.

For example, we will take a text field (it will act as a field for entering a title) and write a small script for it, which, when entering a sentence with a lowercase letter, makes it capitalize it:

$(document).ready(function() ( $(".content").on("keyup", function() ( var text = $(this).val(); var new_text = text.charAt(0) .toUpperCase() + text.substr(1); $(this).val(new_text); )); ));

The script works both when writing text and when simply inserting it. Don't forget that for scripts to work on your site, you must have the jQuery library enabled.

In PHP there is a function ucfirst(), which capitalizes the first letter in a line, ucwords() - capitalizes the letters in all words of the line, problems arise when working with Cyrillic and Unicode.

Cyrillic and Unicode are an eternal problem for everyone PHP versions, the problem is partially solved, there is a function string mb_convert_case (string str, int mode [, string encoding]), which takes as parameters a string, conversion mode (0 - all letters to uppercase, 1 - all letters to lowercase, 2 - ALL FIRST letters of all words in upper case) and encoding.

Converting letters

Task: convert the first letter in a string and all the first letters in all words in a string.

English letters

There are no problems with English letters in standard encodings (UTF-8 and Windows-1251).

Result on screen
First letters
First Letters

Cyrillic and Windows-1251

There should also be no problems with the Cyrillic alphabet in Windows-1251.

Result on screen
First letters
First Letters

Cyrillic and UTF-8

The ucfirst() and ucwords() functions will not cope with the Cyrillic alphabet in Unicode and no conversion will occur.

To do this, the function mb_ucfirst(string str [, string encoding]) is defined, which will process Unicode strings.

Result on screen
first letters
first letters
First letters
First Letters

Quite rarely, but still the need arises, how to capitalize the first letter in php Cyrillic.

This is when the first letter of a word becomes capitalized. This can be used, for example, to unify the spelling of a username, or, for example, when you need to automatically compose text into a sentence.

How to capitalize the first letter php latin

Everything is quite simple here: PHP has 2 functions: ucfirst() and ucwords(). The first capitalizes only the first letter in a line, the second capitalizes the first letter of each word in a line.

// string $str = "first letters"; // make the first letter uppercase echo ucfirst($str) . "


First letters
And
First Letters

We see the difference. There will be no problems with English texts (or any others) written in Latin.

How to capitalize the first letter php cyrillic Windows-1251 (CP-1251)

There will also be no big problems with Cyrillic (Russian letters) written in Windows-1251 encoding:

// string $str = "first letters"; // make the first letter uppercase echo ucfirst($str) . "
"; // first letter of all words echo ucwords($str);

As a result, we get two lines:
First letters
And
First Letters

How to capitalize the first letter php cyrillic UTF-8

But as soon as it comes to UTF-8, problems begin, because the Cyrillic alphabet in UTF-8 takes 2 bytes, and therefore nothing will work. To do this, we will use the “crutch” from Multibyte String Functions. If this plugin is installed in PHP, then you can simply use 2 similar functions: mb_ucfirst and mb_convert_case.

And if they are not there, then you need to supplement the code with your own alternatives:

If (!function_exists("mb_ucfirst") && extension_loaded("mbstring")) ( /** * mb_ucfirst - converts the first character to uppercase * @param string $str - string * @param string $encoding - default encoding UTF-8 * @return string */ function mb_ucfirst($str, $encoding="UTF-8") ( $str = mb_ereg_replace("^[\ ]+", "", $str); $str = mb_strtoupper( mb_substr($str, 0, 1, $encoding), $encoding) mb_substr($str, 1, mb_strlen($str), $encoding); return $str; ) ) $str = "first letters"; // try to convert the Cyrillic alphabet into Unicode using the ucfirst echo ucfirst($str) function. "
"; // try to convert the Cyrillic alphabet into Unicode using the ucwords echo ucwords($str) function. "
"; // processed by the declared function mb_ucfirst() echo mb_ucfirst($str) . "
"; // convert using the mb_convert_case function echo mb_convert_case($str, MB_CASE_TITLE, "UTF-8");

The result of this code will be the following lines.

Often, in a hurry when adding materials to a site or, for example, creating a new topic on a forum, a user may start writing a sentence (title) with a small (lowercase) letter. This is to some extent a mistake.

I will show several options for solving this problem: PHP and CSS are more suitable for already published materials, when jQuery can correct the situation before publication.

First letter of a string in uppercase in PHP

In PHP there is a function called “ucfirst”, which just converts the first character of a line to uppercase, but its disadvantage is that it does not work quite correctly with Cyrillic.

To do this, we will write our own small function. The implementation would look like this:

In this version, we will receive a sentence that begins with a capital letter, which, in fact, is what we need.

Uppercase first letter of a string in CSS

This method visually (that is, the suggestions will appear as they are in the site's source code) also converts the first character to uppercase.

The usage is as follows:

first sentence

second sentence

third sentence

fourth sentence

#content p:first-letter ( text-transform: uppercase; )

Using the “first-letter” pseudo-element and the “text-transform” property, we set the design for each first letter of the paragraph.

First letter of a string in uppercase in jQuery

As I said earlier, this conversion method is best suited for materials that are yet to be published.

For example, we will take a text field (it will act as a field for entering a title) and write a small script for it, which, when entering a sentence with a lowercase letter, makes it capitalize it:

$(document).ready(function() ( $(".content").on("keyup", function() ( var text = $(this).val(); var new_text = text.charAt(0) .toUpperCase() + text.substr(1); $(this).val(new_text); )); ));

The script works both when writing text and when simply inserting it. Don't forget that for scripts to work on your site, you must have the jQuery library enabled.