substr() function is used to cut any part from a string in PHP. It is a built-in function of PHP that returns the portion of a string based on the starting position and the length value. The original value of the string remains unchanged after applying this function to that value. How the substr() function can be used in PHP script to cut the portion of a string is shown in this tutorial.

Syntax:

The syntax of the substr() function is given below with an explanation.

string substr (string $string, int $offset [, int|null $length = null ])

This function can take three arguments. Two arguments are mandatory, and one argument is optional. The first argument, $string, takes the original string value from where the substring will be extracted. The second argument, $offset, takes the starting position from where the substring will extract. The value of this argument can be any positive or negative integer. The optional argument, $length, takes the length of the substring. The value of this argument can be any positive or negative number. If this argument is omitted from the substr() function, then this function will return the string starting from $offset value to the end of the original string.

Example 1: Use of substr() with the positive starting position

This example shows how substr() function can be used with the positive starting position only. If the starting position is positive, then the counting will start from 0. Here, the starting position is 11. So, the string will be cut from the 12th position to the end of the of the original string.

<?php


    //Initialize the original string


    $string = “Welcome to LinuxHint”;


    //Cut the substring starting from 11 where the counter starts from 0


    $sub_string = substr($string, 11);


    //Print the original string and the substring


    echo

The original string value is: $string

;


    echo

The substring value is: $sub_string

;

?>

Output:

The following output will appear after running the script from the webserver. It shows that LinuxHint is the substring after applying the substr() function.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/01/echo/Extract-substring-from-the-string-in-PHP-using-substr_1.jpg" data-lazy- height="226" src="data:image/svg xml,” width=”1087″>

Example 2: Use of substr() with the negative starting position

This example shows how the substr() function can be used with the negative starting position to cut a string from another string value. If the starting position is negative, then the counting starts from the end of the original string, and the value 1. No length argument is given. So, the script will return a substring from the starting position to the end of the string.

<?php


    //Initialize the original string


    $string = “I like programming”;


    //Cut the substring starting from 11 where the counter starts from 0


    $sub_string = substr($string, 11);


    //Print the original string and the substring


    echo

The original string value is: $string

;


    echo

The substring value is: $sub_string

;

?>

Output:

The following output will appear after running the script from the webserver. It shows that programming is the substring after applying the substr() function.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/01/echo/Extract-substring-from-the-string-in-PHP-using-substr_2.jpg" data-lazy- height="221" src="data:image/svg xml,” width=”1088″>

Example 3: Use of substr() with the positive starting position and length

This example shows how the substr() function can be used with a positive starting position and length. Here, the starting position is 21, and the length is 9. The starting position will be retrieved by counting from 0 to 21, and the ending position will be retrieved by counting 9 characters.

<?php


    //Initialize the originl string


    $string = “PHP is a server-side scripting language”;


    //Cut the substring starting from 21 with length 9


    $sub_string = substr($string, 21, 9);


    //Print the original string and the substring


    echo

The original string value is: $string

;


    echo

The substring value is: $sub_string

;

?>

Output:

The following output will appear after running the script from the webserver. Scripting is the substring after applying the substr() function.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/01/echo/Extract-substring-from-the-string-in-PHP-using-substr_3.jpg" data-lazy- height="199" src="data:image/svg xml,” width=”1090″>

Example 4: Use of substr() with the negative starting position and length

This example shows how the substr() function can be used with the negative starting position and length. Here, the starting position is -16, and the length is -9. Both the starting position and the length will count from the end of the string and the counting will start from 1.

<?php


    //Initialize the original string


    $string = “HTML is a mark-up language”;


    //Cut the substring starting from -16 with the length -9


    $sub_string = substr($string, 16, 9);


    //Print the original string and the substring


    echo

The original string value is: $string  

;


    echo

The substring value is: $sub_string

;

?>

Output:

The following output will appear after running the script from the webserver. It shows that the mark-up is the substring after applying the substr() function.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/01/echo/Extract-substring-from-the-string-in-PHP-using-substr_4.jpg" data-lazy- height="215" src="data:image/svg xml,” width=”1085″>

Example 5: Use of substr() with a negative starting position and positive length

This example shows how the substr() function can be used with the positive starting position and negative length. Here, the starting position is -11, and the length is 7. The starting position will be retrieved by counting from the end of the string. The substring will be generated by counting 7 characters after the starting position.

<?php


    //Initialize the originl string


    $string = “Javascript is very popular now”;


    //Cut the substring starting from -11 with the length 7


    $sub_string = substr($string, 11, 7);


    //Print the original string and the substring


    echo

The original string value is: $string

;


    echo

The substring value is: $sub_string

;

?>

Output:

The following output will appear after running the script from the webserver. It shows that popular is the substring after applying the substr() function.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/01/echo/Extract-substring-from-the-string-in-PHP-using-substr_5.jpg" data-lazy- height="228" src="data:image/svg xml,” width=”1088″>

Example 6: Use of substr() with a positive starting position and negative length

<?php


    //Initialize the originl string


    $string = “Angular 8 is a Javascript framework”;


    //Cut the substring starting from 11 where the counter starts from 0


    $sub_string = substr($string, 0, 26);


    //Print the original string and the substring


    echo

The original string value is: $string

;


    echo

The substring value is: $sub_string

;

?>

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/01/echo/Extract-substring-from-the-string-in-PHP-using-substr_6.jpg" data-lazy- height="221" src="data:image/svg xml,” width=”1090″>

Conclusion

All possible combinations of using the substr() function in PHP to cut a string from another string are shown in this tutorial using six simple examples. Hopefully, the readers can apply the substr() function efficiently after practicing the examples of this tutorial.

About the author

<img alt="Fahmida Yesmin" data-lazy-src="https://kirelos.com/wp-content/uploads/2021/01/echo/channel-logo-150×150.jpg5ffc2de712391.jpg" height="112" src="data:image/svg xml,” width=”112″>

Fahmida Yesmin

I am a trainer of web programming courses. I like to write article or tutorial on various IT topics. I have a YouTube channel where many types of tutorials based on Ubuntu, Windows, Word, Excel, WordPress, Magento, Laravel etc. are published: Tutorials4u Help.