Online consultation on website maintenance in Ulyanovsk: ICQ# 179104682

How to truncate a cyrillic string with using PHP

Article placement date: 06/01/2014

Creating a site in Ulyanovsk with serious functionality and interaction with databases very often requires that a certain line does not exceed a certain specified volume. This may be necessary, for example, to limit the number of characters transmitted to the database server using the input field, to display only part of the line on the site page (for example, when generating news announcements of equal height in the list) and for many other tasks.

The first thing novice programmers stumble upon is PHP function substr. It is really used for string trimming and its syntax is simple. substr(string, int start [, int length]), where int start is the starting character from which to start cutting the string, and the optional parameter int length is the number of characters to cut. However, for an inexperienced programmer, it may seem strange if he tries to use it for PHP trimming a Cyrillic string. As a result of the operation of substr with a Cyrillic string, a question mark, or a question mark in a rhombus, may appear at the end of the truncated string, and total number for some reason, the cut characters are two times less than what was specified in the int length parameter. Why is this happening?

The fact is that Russian-language characters in UTF-8 have a size of 2 bytes, and Latin characters are only 1 byte. The PHP substr function cuts a string exactly by bytes, not by characters. If the string consists of Latin characters, then nothing strange happens, since the number of characters is the same as the number of bytes. And when working with Cyrillic, where each character occupies 2 bytes, the int length parameter can easily fall into the “middle” of the character, and as a result, at the end of the cut line, we will see that ill-fated question mark in a rhombus at the end of the cut line.

How to properly cut a part of a string with Cyrillic?

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

The function syntax is simple:

iconv_substr(string, int start[, int length[, charset]])

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

$new_string = iconv_substr ($string, 0 , 80 , "UTF-8");

The number zero in the example means that the count of 80 characters starts from the very beginning of the line.

Responsive layout of the site involves an adequate change of its pages depending on the device used by the visitor. If with regard to block and inline elements, almost everything lends itself to “automation” using CSS and JavaScript, then when creating content and using databases, it is difficult to do without the server side.

We usually trim a string in PHP when HTML element layout has a limit on the number of characters that are visible, but this is a private task.

Traditional Solution

One of the most popular and frequently used functions is substr(). It is passed two or three parameters:

    original string; start position ($iPos); the length of the substring to be cut ($iLen).

The last parameter can be omitted. If only two parameters are specified: the result of substr() will be a substring, from the start position ($iPos) to the end original string. If three parameters were passed, then truncate the PHP string from the start position ($iPos) to the specified length ($iLen).

Selecting only the beginning of a string with this function is possible when the first parameter is zero. If the starting position is negative, then PHP will consider the character at the $iPos position from the end of the string as the start from which to cut the string. Numbering of characters in a line is carried out from zero.

special function

PHP considers the task of "cutting a string" (in a broad sense) in context: from both sides. Historically, this is the trim() function, which aims to remove insignificant characters:

    spaces; line breaks; carriage return; tabulation; null characters

from both ends of the line. This is a very requested feature, especially when working with databases, the selection of which often contains a lot of gaps. When using the explode() function, extra characters are also often obtained from the beginning and end of the string.

However, not all developers use the trim() function to its full potential. For the "trim the string" task, PHP suggests using the second parameter of the function, where you can specify any set of characters that should be eliminated from the original string.


It is essential that characters are removed only from the beginning and from the end of the original string. It is important that by manipulating the masks of characters to be deleted and the sequence of subtasks.

    First, we trim the string in PHP by one mask. Then on the other. Then on the third.

As a result of the sequence of options for using one function, we cut off the text in PHP as it is required to solve the task.

Non-standard ways

If we do not consider the whole range of string functions of the language, then the explode()/implode() pair of functions and the str_replace() function allow solving non-standard tasks of "cutting" string information.


The result of a MySQL query is always formal in the structure of the output information, and the content of the fields (elements) of the result is always determined by the source, that is, the developer who designed the database.

This is a particular example, but it accurately answers the question: how to cut a string in PHP when there are a lot of strings. The trm() function is a particularity, and its history is determined by tasks when there was not much information, and there was no need to cut off something other than insignificant characters.

Today, information circulates in large volumes and cutting line by line is not only unnecessary, but also irrational.

Splitting into components is also an option on how to trim a string. PHP will automatically explode () will cut one large text into many necessary lines. Using the str_replace() function - that is, replacing one occurrence of characters with another - you can achieve a similar effect.


The dynamics and volume of information that needs to be processed is, first of all, an adequate solution, and not the use of one special function.

Programmers very often have to deal with various string manipulation functions in php. All functions are presented in various reference books, but I would like to limit myself to only the main ones.

PHP function substr - get part of a string

When you need to select a passage of a given length from a string, starting with given position, the substr function comes to the rescue.In this function, a comma is passed: text, starting position and number of characters. The last parameter is optional, and if it is absent, the result of the function will be a fragment of the source text from the start position to the end of the line.

PHP function strlen - get the length of a string

This function returns the length of the original string as an integer.With this function, you can check the length of the data entered by the user, and maybe something else.

PHP trim function - remove extra spaces from the edges of a string

The trim function removes any whitespace characters from the edges of a string, including the newline character. There are also rtrim and ltrim functions that remove spaces at the end and/or the beginning of a string.

PHP function strpos - searches within a string

The strpos function looks for a substring in a string and, if successful, returns the position of the beginning of that substring. After finding the first substring, the search stops.The order of the arguments in the function, you guessed it, is: source string, substring, starting position. The third parameter is optional, but try to remember what it is.

PHP function strip_tags - strips HTML and PHP tags from a string

The strip_tags function returns a string stripped of html tags and php. It will come in handy when you write a comment module on your site, so as not to leave the attackers the opportunity to hack your site through a form.html and php must be removed"; echo strip_tags($text); echo strip_tags($text," ");//do not remove em tags ?> The first argument of the function is the source text, and the second is the tags that should not be removed.

PHP function strtolower - Converts a string to lowercase

PHP function strtoupper - convert string to upper case

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

The site assumes an adequate change of its pages depending on the device used by the visitor. If with respect to block and inline elements, almost everything lends itself to “automation” using CSS and JavaScript, then when generating content and using databases, it is difficult to do without the server side.

We usually truncate a string in PHP when the HTML layout element has a limit on the number of characters that are visible, but this is a private matter.

Traditional Solution

One of the most popular and frequently used functions is substr(). It is passed two or three parameters:

  • original string;
  • start position ($iPos);
  • the length of the substring to be cut ($iLen).

The last parameter can be omitted. If only two parameters are given: the result of substr() will be a substring, from the start position ($iPos) to the end of the source string. If three parameters were passed, then truncate the PHP string from the start position ($iPos) to the specified length ($iLen).

Selecting only the beginning of a string with this function is possible when the first parameter is zero. If the starting position is negative, then PHP will consider the character at the $iPos position from the end of the string as the start from which to cut the string. Numbering of characters in a line is carried out from zero.

special function

PHP considers the task of "cutting a string" (in a broad sense) in context: from both sides. Historically, this is the trim() function, which aims to remove insignificant characters:

  • spaces;
  • line breaks;
  • carriage return;
  • tabulation;
  • null characters

from both ends of the line. This is a very requested feature, especially when working with databases, the selection of which often contains a lot of gaps. When using the explode() function, extra characters are also often obtained from the beginning and end of the string.

However, not all developers use the trim() function to its full potential. For the "trim the string" task, PHP suggests using the second parameter of the function, where you can specify any set of characters that should be eliminated from the original string.

It is essential that characters are removed only from the beginning and from the end of the original string. It is important that by manipulating the masks of characters to be deleted and the sequence of subtasks.

  • First, we trim the string in PHP by one mask.
  • Then on the other.
  • Then on the third.

As a result of the sequence of options for using one function, we cut off the text in PHP as it is required to solve the task.

Non-standard ways

If we do not consider the whole range of string functions of the language, then the explode()/implode() pair of functions and the str_replace() function allow solving non-standard tasks of "cutting" string information.

The result of a MySQL query is always formal in the structure of the output information, and the content of the fields (elements) of the result is always determined by the source, that is, the developer who designed the database.

This is a particular example, but it accurately answers the question: how to cut a string in PHP when there are a lot of strings. The trm() function is a particularity, and its history is determined by tasks when there was not much information, and there was no need to cut off something other than insignificant characters.

Today, information circulates in large volumes and cutting line by line is not only unnecessary, but also irrational.

Splitting into components is also an option on how to trim a string. PHP will automatically explode () will cut one large text into many necessary lines. Using the str_replace() function - that is, replacing one occurrence of characters with another - you can achieve a similar effect.

The dynamics and volume of information that needs to be processed is, first of all, an adequate solution, and not the use of one special function.