How to cut text from a larger string in PHP

If you need to cut a text string from a larger one in PHP you are in the right place. In this article I am going to show you how to perform this functionality in a simple way in PHP: with the substr function.

We will also see, as an extra, how to cut strings in texts with special characters like our beloved letter Ñ.

Content

  • Data to cut text with the substr function
  • Get a part of a string with PHP
    • 1. Normal use of PHP’s substr function
    • 2. Alternative way to use substr() with negative trailing position
    • 3. Alternative form of substr() with negative initial position
    • 4. Cut from negative starting position to the end
  • Extra: cut text with UTF-8 characters

Data to cut text with the substr function

To extract a part of a text string in php we need to know three pieces of information:

  1. What is the text string that contains the substring.
  2. In which position the text to be cut begins.
  3. How many characters we want to cut.

With these three pieces of information, you can now obtain a substring of a larger text string.

Get a part of a string with PHP

As I mentioned before, to obtain a part of a text in PHP we will generally use the substr() function. This function is capable of extracting a portion of text from a larger text with the three aforementioned data.

Knowing this, in the following lines I will explain the two functionalities that the function allows to perform substr from PHP:

  1. The formal use of the substr function, that is, to extract a portion of a text from a starting position to a final one.
  2. Cut text from start to end LESS a specified number of characters.
  3. Cut text starting at the initial character calculated as follows: final position minus indicated number of characters.
  4. And the alternative way to cut the last X characters of a text.

1. Normal use of PHP’s substr function

Let’s first see what is the definition of the substr function according to the PHP manual:

string substr ( string $string , int $start [, int $length ] )

Now in a simplified way:

cadena_cortada = substr( cadena_de_texto, posicion_caracter_inicial, [longitud_del_texto_a_cortar]);

The function substr() from PHP returns the “cut off” part of the text with the data supplied as the second and third parameters (initial position and string length).

The third parameter of the function is optional (that’s why I’ve indicated it in square brackets). If you don’t enter the third parameter, the function will cut the text from start_char_position to the end of text_string.

Example of normal use of the substr function

<?php
$cadena = “Esta es el texto principal”;
$subcadena = substr ( $cadena, 0, 4);
echo $subcadena; //imprime: Esta
?>

In the example I have cut the first four characters of the string $string. The “0” tells it where I want it to start cutting the text and the 4 tells it that the text I want to get is 4 characters long.

2. Alternative way to use substr() with negative trailing position

there is a second way to use the function substr of PHP, and this is indicating a negative integer in the third parameter of the function. With this slight change we indicate to the function that it must cut from the initial position to the final position of the text LESS the indicated characters.

<?php
$cadena = "Esta es el texto principal";
$subcadena = substr ( $cadena, 0, -4);
echo $subcadena; //imprime: Esta es el texto princ
?>

This way of using the function substr is incredibly useful when we want to remove a final part of a word or text. What in For example:

<?php
$cadena = "mensaje-ESP";
$subcadena = substr ( $cadena, 0, -4);
echo $subcadena; //imprime: mensaje
?>

3. Alternative form of substr() with negative initial position

We also have the possibility of start cutting the string by counting characters from the end position of the string. To do this, we are going to indicate a negative value in the initial position that will be calculated counting from the end of the string.

Let’s see an example:

<?php
$cadena = "mensaje-ESP";
$subcadena = substr ( $cadena, -4, 2);
echo $subcadena; //imprime: -E
?>

4. Cut from negative starting position to the end

Finally, let’s look at the alternate way to cut some final characters knowing its length. A really useful way of using substr when we want to cut some suffix known to us. An example would be cut the extension of an image:

<?php
$cadena = "mi-imagen.jpg";
$subcadena = substr ( $cadena, -3);
echo $subcadena; //imprime: jpg
?>

In this example you will have seen how I have obtained the last three characters by indicating a negative value in the initial character parameter, in this way, the substr function has started to cut from (final position – 3).

Extra: cut text with UTF-8 characters

If you need cut character strings in texts made up of special characters (UTF-8) I recommend you to forget the function substr. If you use the function substr to cut in these texts you will get unexpected results, since it will often count a single special character as several, resulting in incorrect counting.

You will need for this purpose the PHP equivalent function for multi-byte characters: the function mb_substr(). This function performs the same functionality as its namesake for ASCII characters, so the usage is similar except for one point, the character encoding of the string that is used.

Let’s see then, as before, the definition from the PHP manual for this function:

string mb_substr ( string $str , int $start [, int $length = NULL [, string $encoding = mb_internal_encoding() ]])

And if I translate it in a more simplified way:

sever-string = mb_substr ( text-string, starting-character-position, [longitud-del-texto-a-cortar], [codificación-de-caracteres]);

Although as mandatory data (those that are NOT in brackets) they are only the data text-string and start-character-positionnormally we will indicate the encoding of the text-string.

Let’s see an example of use:

<?php
$texto = ‘Lección con UTF-8’;
$cadena_cortada = mb_substr($texto, 8, null, "UTF-8");
echo $cadena_cortada; //imprime “con UTF-8”
?>

this little script is similar to the previous examples with the particularity that I have indicated the UTF-8 encoding to interpret the characters used in $text. Also, as a particularity, you will see that I have indicated NULL in the third parameter so that it is ignored (and not used), since I want to cut from character 8 to the end.

Note: As additional information, comment that if we do not indicate the encoding in the function mb_substr, the one indicated by default in the system will be used with, for example, the function mb_internal_encoding(“UTF-8”);

Leave a Reply