the Internet Windows Android

Only the first letter Capital PHP. How to make the first letter of the title PHP Cyril

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.

In PHP there is a function ucfirst (), which is the first letter in the line in the title, UcWords () - makes capital letters in all words of the line, in working with Cyrillic, in Unicode, problems arise.

Cyrillic and Unicode - the eternal problem of all PHP versions, partially solved problem, there is a String MB_Convert_Case function (String Str, Int Mode [, String Encoding], which takes a string as parameters, conversion mode (0 - all letters to the top register, 1 - All letters to the lower case, 2 - all the first letters of all words to the top register) and encoding.

Transformation of letters

Task: Convert the first letter in the string and all the first letters in all words in the line.

English letters

With English letters in standard-used encodings (UTF-8 and Windows-1251) there are no problems.

"; // First letter in all words Echo UcWords ($ STR);?\u003e

Result on screen
First Letters.
First Letters.

Cyrillic and Windows-1251

With Cyrillic in Windows-1251, problems should also not arise.

"; // First letter in all words Echo UcWords ($ STR);?\u003e

Result on screen
First letters
First letters

Cyrillic and UTF-8

With Cyrillic in Unicode, the UCFirst () and UCWORDS () function will not cope and the transformations will not happen.

To do this, the MB_UCFIRST feature is defined (String Str [, String Encoding]), which will handle unicode strings.

"; // Try Cyrillic in Unicode to convert UCWORDS ECWO UCWORDS ($ STR)."
"; // Processing the declared function MB_UCFIRST () ECHO MB_UCFIRST ($ STR)."
"; // Transform MB_CONVERT_CASE ECHO MB_CONVERT_CASE ($ STR, MB_CASE_TITLE," UTF-8 ");?\u003e

Result on screen
first letters
first letters
First letters
First letters

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.

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.

It is rare quite enough, but still such a need arises, how to make the first letter of the title PHP Cyril.

This is when the first letter of the word becomes big. Apply this can, for example, to unify writing a username, or, for example, when you need to automatically compile text in the offer.

How to make the first letter of the title PHP Latin

Everything is simple enough: there are 2 functions in PHP: UCFirst () and UCWORDS (). The first makes only the first letter in the title string, the second makes the first letter of each word in the row of the title.

// Row $ str \u003d "First Letters"; // The first letter to the upper case of Echo Ucfirst ($ STR). "


First Letters.
and
First Letters.

We see the difference. With English texts (or by any other), there will be no problems written by the Latin.

How to make the first letter of the title PHP Cyril Windows-1251 (CP-1251)

With Cyril (Russian letters) that are recorded in the Windows-1251 encoding, too, there will be no big problems:

// Row $ str \u003d "first letters"; // The first letter to the upper case of Echo Ucfirst ($ STR). "
"; // First letter in all words Echo UcWords ($ STR);

As a result, we get two lines:
First letters
and
First letters

How to make the first letter of the title PHP Cyrillet UTF-8

But as soon as it comes to UTF-8, problems begin, because Kyrilitsa in UTF-8 takes 2 bytes, and therefore nothing happens. For this we will use the "crutch" from MultiByte String Functions. If this plugin is installed on PHP, you can simply use 2 similar functions: MB_UCFIRST and MB_CONVERT_CASE.

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

If (! Function_exists ("MB_UCFIRST") && extension_loaded ("MBString")) (/ ** * MB_UCFIRST - Converts the first character to the top register * @Param String $ STR - line * @Param String $ Encoding - Coding, default UTF-8 * @return String * / Function MB_UCFIRST ($ str, $ Encoding \u003d "UTF-8") ($ STR \u003d MB_EREG_REPLACE ("^ [\\] +", "", $ STR); $ STR \u003d MB_STRTUPPER ( MB_SUBSTR ($ str, 0, 1, $ encoding), $ encoding). MB_Substr ($ str, 1, MB_STRLEN ($ STR), $ Encoding); Return $ str;)) $ str \u003d "first letters"; // Try Cyrillic in Unicode to convert UCFirst ECHO UCFIRST ($ STR) function. "
"; // Try Cyrillic in Unicode to convert UCWORDS ECWO UCWORDS ($ STR)."
"; // Processing the declared function MB_UCFIRST () ECHO MB_UCFIRST ($ STR)."
"; // Transform the MB_CONVERT_CASE ECHO MB_CONVERT_CASE ($ STR, MB_CASE_TITLE," UTF-8 ") function;

The result of this code will be such lines.

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.

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.