The php function raises the first letter of case. Convert first letters to capital letters (upper case) - PHP

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 in all versions of PHP, the problem has been partially solved, there is a function string mb_convert_case (string str, int mode [, string encoding]), which takes as parameters a string, a conversion mode (0 - all letters to uppercase, 1 - all letters in lower case, 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

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

There is a function in PHP called “ucfirst”, which just converts the first character of a line to upper case, 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 small letter, makes it with a capital letter:

$(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.

Making the first letter capitalize is a fairly common task for web developers. This may be required in a variety of cases, but we will not focus on them; our task is to raise the first letter in any word with any encoding.

It’s not for nothing that I mentioned the encoding, because PHP has a built-in function for this, this is ucfirst(). But it doesn’t suit us, because... only works with Latin characters. Those. if you use this function to try to raise the first letter in the word “home”, then everything will be fine and you will get “Home” as an output. And if you do the same with the word “house”, then the output will remain the same as “house” with a small letter. This happens because we use Russian characters in UTF-8 encoding, i.e. multi-byte, and ucfirst() is used for single-byte encodings.

PHP has functions for working with strings in multibyte encodings, and we use them in the function presented below to capitalize the first letter in a multibyte encoding.

A function that capitalizes the first letter /** * Uppercase first letter. Working with multi-byte encodings. * * @param $str * @param string $encoding * @return string */ function upFirstLetter($str, $encoding = "UTF-8") ( return mb_strtoupper(mb_substr($str, 0, 1, $encoding), $encoding) . mb_substr($str, 1, null, $encoding) echo upFirstLetter("home"); // will print "Home"

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

There is a function in PHP called “ucfirst”, which just converts the first character of a line to upper case, 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 small letter, makes it with a capital letter:

$(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.