PHP substr Russian letters. Cut the PHP string: simple ways. PHP Strip_TAGS - Removes HTML and PHP Tags from Row

Online advice on site maintenance issues in Ulyanovsk: icq # 179104682

How to trim the Cyrillic string with PHP

Placement date Articles: 01.06.2014

The creation of a site in Ulyanovsk with a serious functionality and interaction with databases very often requires a certain string to exceed a certain specified volume. This may be needed, for example, to limit the number of characters that are transmitted to the database server using the Input field, to display only part of the line on the site, on the site page (for example, in the formation of equal news announcements in the list) and for many other tasks.

The first thing that start-up programmers are stumbled - PHP SUBSTR function. It is really used to trim strings and syntax it is easy. substr.(String, Int Start [, Int Length]), where int start - the initial symbol from which you want to start cutting the string, and the optional parameter int Length - the number of characters you want to cut. However, for an inexperienced programmer, it may seem strange if he is trying to use it for PHP trimming strings with Cyrillic. As a result of the work of Substr with a Cyrillic string at the end of the cropped line, a question mark may appear, or a question mark in Rhombic, and for some reason, the total number of cut characters is twice as few of what was specified in the INT Length parameter. Why is it going on?

The point in that Russian-speaking characters in UTF-8 have a size of 2 bytes, and Latin characters are only 1 byte. PHP The Substr function cuts the line by bytes, and not by symbols. If the string consists of Latin characters, then nothing strange occurs, since the number of characters coincides with the number of bytes. And when working with Cyrillic, where each character takes 2 bytes, the int length parameter easily can get to the "middle" of the character, and as a result, at the end of the cut line in the output, we will see that ill-fated question mark in Rhombic.

How to cut part of a row with Cyrillic?

The solution to this problem is actually very simple. For correct PHP trimming strings containing Cyrillic characters you need to use PHP function iconv_substr

The syntax function is simple:

iconv_substr (String, Int Start [, Int Length [, Charset]])

Example for trimming a Russian-language string up to 80 characters with UTF-8 encoding using iconv_substr:

$ new_string \u003d iconv_substr ($ String, 0, 80, "UTF-8");

The number of zero in the example means that the countdown of 80 characters begins from the very beginning of the string.

Programmers very often have to deal with different functions of working with rows in PHP. All functions are presented in different directories, but I would like to limit ourselves to only the main ones.

PHP SUBSTR function - get part of the string

When you need to select an excerpt of a given length starting from a given position, the Substr function comes to the rescue. In this function, over the comma is transmitted: text, starting position and number of characters. The latter parameter is not required, and in case of its absence, the result of the function will be an excerpt of the source text from the starting position until the end of the row.

PHP Strlen function - learn string length

This function returns the length of the source string in the form of an integer. This feature you can check the length of the data entered by the user, and maybe something else.

PHP TRIM function - remove excess spaces around the edges of the line

The Trim feature removes any space characters along the edges of the string, including the line translation symbol. Also, there are the functions of RTRIM and Ltrim, which remove the gaps to the baton at the beginning of the string.

PHP STRPOS function - Searches in a string

The STRPOS function is looking for a substring in the string and, if success, returns the position of the start of this substring. After finding the first substring, the search stops. The order of arguments in the function, as you have already appreciated, is: the source line, substring, starting position. The third parameter is not required, but try to remember what it is.

PHP Strip_TAGS - Removes HTML and PHP Tags from Row

The Strip_TAGS function returns a string cleaned from HTML and PHP tags. She will be useful for you when you write the comments module on your site, so that you would not leave the attacks to hack your site through the form.hTML. and php. Must be deleted "; Echo strip_tags ($ text); Echo strip_tags ($ text," "); // Do not delete EM tags?\u003e The first argument of the function is the source text, but the second tags that can not be deleted.

PHP STRTOLOWER function - translates the string to the Lower Register

PHP STRTUPPER function - translates the string to the upper case

If this site was useful to you, you can help in its development, putting

The adaptive layout of the site involves an adequate change in its pages depending on the device that the visitor uses. If almost everything is amenable to "automation" with CSS and JavaScript to the block and lowercase elements, then in the formation of content and use databases without server side it is difficult to do.

Usually cutting a row on PHP when the HTML layout element has a limit on the number of characters that are visible, but this is a private task.

Traditional decision

One of the most popular and frequently used features is substr (). Two or three parameters are transmitted to it:

    source line; Beginning position ($ IPOS); The length of the cut-out substring ($ Ilen).

The last parameter can not be indicated. If only two parameters are specified: the result of substr () will be a substring, from the start position ($ ipos) to the end of the source line. If three parameters were transmitted, then cut the PHP string from the start position ($ ipos) to the specified length ($ Ilen).

Select only the beginning of the string by means of this function is possible when the first parameter is zero. If the initial position is negative, then the beginning, from which we cut a string, PHP will count the character at the $ IPOS position from the end of the line. The numbering of characters in the string is carried out from scratch.

Special function

The task "Cut the line" PHP considers (in a broad sense) in context: on both sides. Historically, this is a trim () function, which intends to remove insignificant characters:

    gaps; Line translations; Return carriage; tabulation; Zero symbols

with both ends of the string. This is a very sought-after function, especially when working with databases, the sample of which often contains many gaps. When using the Explode () function, unnecessary characters from the beginning and end of the string are also often obtained.

However, not all developers use the Trim () function in full force. For the task, "Cut the string" PHP proposes to use the second parameter of the function where you can specify any set of characters that should be eliminated from the source line.


It is significant that the characters are removed only from the beginning and from the end of the source line. It is important that manipulating the masks of the removable characters and the sequence of subtasks.

    First cutting a string on PHP on one mask. Then on the other. Then third.

As a result of the sequence of application options for one function, we cut the text on PHP as required to solve the task.

Non-standard methods

If you do not consider the entire spectrum of the lowercase functions of the language, then the pair of functions explode () / implode () and the STR_REPLACE () function allows you to solve non-standard tasks "cutting" lower case information.


The result of the MYSQL query is always formulated in the structure of the issued information, and the content of the results (elements) of the result is always determined by the source, that is, the developer who designed the database.

This is a private example, but exactly answering the question: how to cut a string in PHP when there are a lot of strings. The TRM () function is particular, and its history is defined by tasks when the information was not so much, but in the need to cut something except for insignificant characters was not necessary.

Today, information circulates large volumes and cutting line is not only no need, but also irrational.

Separation to the components is also an option how to trim the string. PHP automatically according to Explode () will reduce one large text into a variety of desired rows. Applying the STR_REPLACE () function - that is, replacing one occurrence of characters to another - one can achieve a similar effect.


The dynamics and amount of information that must be processed is primarily an adequate solution, and not the use of one special function.

Did you like the article? To share with friends: