Verification: a143cc29221c9be0

Php capitalize first letter кириллица

The first letter of the string in the upper case on PHP

On PHP there is a function called " ucfirst.", Which just converts the first string symbol in uppercaseBut her minus is that it does not work quite correctly with Cyrillic.

To do this, we will write our small feature. Implementation will look like this:

In this option, we will receive an offer that begins with a capital letter, which, in fact, we need.

The first letter of the string in the upper case on CSS

This method is visually (that is, in source code The proposals will look like it) also converts the first character to the top register.

Use the following:

first sentence

second offer

third offer

fourth sentence

With the help of a pseudo-element " first-Letter.»And the properties" text-transform"We set design for each first letter of paragraph.

The first letter of the string in the upper case on jQuery

As I said earlier, this method of transformation is best suited for materials that will still be published.

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

The script is triggered both when writing text and its easy insert. Do not forget that for the work of the scripts on your site you need a connected jQuery library.

PHP is good by the variety of native language functions. However, in some cases it has to add missing functionality. This is especially noticeable when working with row conversion functions and various encodings.

For example, UCFIRST is a function that converts the first string symbol to the top register. It would seem that no problems should arise, but when working with Cyrillic, this transformation does not occur. This also applies to the UCWORDS functions - the first character of each word in the row is converted to the top register.

With characters eng. Alphabet no problems arise:

"; // Converts the first symbol of each word to the top register in the Echo UCWORDS string ($ ST);?\u003e

Test String Test String

But with Cyrillic problems arise:

"; // Converts the first symbol of each word to the top register in the Echo UCWORDS string ($ ST);?\u003e

Test Row Test Row

For PHP typical cases when the functions are "bad" or do not work with Cyrillic at all. Some functions with the MB prefix solve problems with Cyrillic. For example, MB_STRTOLOWER - bringing the string to the lower register. Unlike STRTOLOWER (), the fact that the character is the letter is determined based on the properties of the Unicode symbol.

To solve the problem, we define the MB_UCFIRST feature (String Str [, String Encoding]), which will handle Unicode characters.

For converts to the top register, the first character of each word in the string is enough to use MB_CONVERT_CASE in MB_Case_Title mode.

Make the first the letter became the title - A rather common task of a web developer. This may be required in a wide variety of cases, but we will not sharpen attention on them, our tasks are to raise the first letter in any word with any encoding.

I did not in vain mentioned about the encoding, because in PHP there is a built-in function so that it is ucfirst (). But it does not fit us, because It works only with Latin characters. Those. If you try to raise the first letter in the word "home", then everything will be fine and you will receive "Home". And if you do the same with the word "house", then at the output as it was "house" with a small letter, and remains. This is because we use Russian characters in utf-8 encoding, i.e. Multibyte, and ucfirst () is used for single-byte encodings.

In PHP there are functions for working with rows in multibyte encodings, then we use them in the function presented below to make the first letter of the title in multibyte encoding.

The function that makes the first letter of the title

/ ** * Uppercase First Letter. Working with Multi-Byte Encodings. * * @Param $ str * @Param String $ Encoding * @return String * / Function Upfirstletter ($ str, $ Encoding \u003d "UTF-8") (RETURN MB_STRTUPPER (MB_SUBSTR ($ str, 0, 1, $ Encoding), $ encoding). MB_Substr ($ STR, 1, NULL, $ Encoding);) Echo UpfirstTer ("House"); // Will "house"

Often in a hurry when adding materials to a website or, for example, creating a new topic on the forum, the user can start writing an offer (name) with a small (lower case) letter. This is to some extent an error.

I will show some options for solving this task: PHP and CSS are more for already published materials when as jQuery will be able to correct the position before the publication.

The first letter of the string in the upper case on PHP

On PHP there is a function called " ucfirst.", Which just converts the first string symbol into the upper case, but minus it is that it does not work quite correctly with Cyrillic.

To do this, we will write our small feature. Implementation will look like this:

In this option, we will receive an offer that begins with a capital letter, which, in fact, we need.

The first letter of the string in the upper case on CSS

This method is visually (that is, in the source code of the site, the sentences will look like it) also converts the first symbol to the upper case.

Use the following:

first sentence

second offer

third offer

fourth sentence

With the help of a pseudo-element " first-Letter.»And the properties" text-transform"We set design for each first letter of paragraph.

Text » Make first character Uppercase, the rest lowercase


Screenshots


Practical tricks on how this can help you

Practical 'real world' examples on our blog that show you how this tool can help you in Excel. Enjoy!

  • Tip: Quickly change the text-case in all selected cells at once

Starting this utility

In the Excel menu choose: ASAP Utilities » Text » Make first character Uppercase, the rest lowercase


« Return to the list of “Text” tools
« Previous
Start first word with Uppercase
 
 Next »
Start Each Word With Uppercase

You can do this too


Download "The one essential add-in for all Excel users."

Additional keywords for this tool:
Change Case, Changing Case, Sentence Case, Capital letter, transform text from without retyping

Screenshots


Practical tricks on how this can help you

Practical 'real world' examples on our blog that show you how this tool can help you in Excel. Enjoy!

  • Tip: Quickly change the text-case in all selected cells at once