PHP string functions trim string. Cut the PHP string: simple ways

The site involves an adequate change in its pages depending on the device that the visitor uses. If almost everything gives up "automation" in relation to block and lowercase elements cSS Tools And JavaScript, then when forming content and using databases without server side, it is difficult to do.

Usually cutting a string on PHP when hTML element The 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 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.

Result query mysql It 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 that 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.

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 solution

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.

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

How to cut a Cyrillic string with using 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 transmitted by the database server using input fields, To display on the site page, only part of the line (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.

Function substr. ($ String, $ Start, $ Length), where $ string is a variable with text, $ START is a character from which the counting starts (for the first character 0), and $ Length is the number of characters of the selected text.

$ Text \u003d "Example and Text";

$ first \u003d substr ($ text, 0,6); // shows 6 characters since the 0th, the very first
echo $ first; // Will "Example".

$ second \u003d substr ($ text, 8.3); // Shows 3 characters since the 8th
Echo $ Second; // Will out "and"
If you do not specify the parameter number of characters (2 digit), then the function will output all the remaining characters.

$ Text \u003d "Example and Text";

$ third \u003d substr ($ text, 8); // will show all characters since the 8th
Echo $ Third; // Will out "and text"

Cut the Russian text using the MB_Substr PHP function

Function mB_Substr. performs the same tasks as substr.but also capable of handling multibyte encodings. Therefore, in the case of Russian text you will be useful. mB_Substr.. It works the same

$ text \u003d "example of text";

$ first \u003d MB_Substr ($ Text, 0.6, "UTF-8"); // shows 6 characters since the 0th, the very first
// Attention! Specify the source code encoding! IN this case UTF-8.
echo $ first; // withdrawn "Example"

$ second \u003d MB_Substr ($ Text, 7.6, "UTF-8"); // Shows 6 characters since the 7th
Echo $ Second; // withdraw "Text"
Please note that in the case of Cyrillic (Russian text), it is better to specify the encoding at the end of the function. In the example, the most popular option is used - UTF-8

Cut the text from the end

If you want to cut text from the end, to do this, specify a negative number for a variable denoting from which symbol the text is started.

$ text \u003d "Sprite and Fanta";

$ first \u003d substr ($ text, -6,5); // Shows 5 characters since the 6th from the end
echo $ first; // Will "FANT"

After trimming, I also finish the text

After trimming the variable with the text add additional text

$ example \u003d "Text Text Text TEXT";

$ EXAMPLE \u003d substr ($ example, 0.9); // shows 9 characters since the 0th, the very first
$ example. \u003d "More ...";
Echo $ Example; // Will out "Text Text More ..."
You can vice versa, to make the recording at the beginning, and then cracked text

$ text \u003d "uncle";

$ example \u003d "George and Michael";
$ EXAMPLE \u003d substr ($ example, 0,6); // shows 6 characters since the 0th, the very first
$ text. \u003d $ example;
Echo $ Text; // Will "Uncle George"

Trimming text to first blank

$ Text \u003d "Hello World!";
Echo Substr ($ Text, 0, Strpos ($ text, "")); // Will "Hello"

Finished PHP text trimming word according to PHP

Function Cut ($ String, $ Length) (
$ String \u003d MB_Substr ($ String, 0, $ Length, "UTF-8"); // Cut and work with all the encodings and indicate the source encoding
$ position \u003d MB_STRRPOS ($ String, "", "UTF-8"); // Definition of the position of the last space. It is on him and share words
$ String \u003d MB_Substr ($ String, 0, $ Position, "UTF-8"); // Cut the variable by position
RETURN $ STRING;
}

$ text \u003d "Northern War ended in 1721";
Echo Cut ($ Text, 17); // withdraw "Northern War", without part of the word "ended"

You can trim according to the words through an array ...

$ Text \u003d "Microsoft was founded by Bill Gates back in 1975.";

$ Array \u003d Explode ("", $ text); // Recycle the string into an array
$ array \u003d array_slice ($ array, 0.5); // Select the first 5 words elements
$ newtext \u003d implode ("", $ array); // Array recycle again in the string

Echo $ newtext; // withdrawn "Microsoft was founded by Bill Gates"