Extract substring from the string in PHP using substr()

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 "<h3> The original string value is:<b style='color:pink'> $string </b></h3>";
    echo "<h3> The substring value is:<b style='color:blue'> $sub_string </b></h3>";
?>

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.

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 "<h3> The original string value is:<b style='color:green'> $string </b></h3>";
    echo "<h3> The substring value is:<b style='color:purple'> $sub_string </b></h3>";
?>

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.

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 "<h3> The original string value is:<b style='color:green'> $string </b></h3>";
    echo "<h3> The substring value is:<b style='color:lightseagreen'> $sub_string </b></h3>";
?>

Output:

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

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 "<h3> The original string value is:<b style='color:MediumVioletRed'> $string   </b></h3>";
    echo "<h3> The substring value is:<b style='color:DeepPink'> $sub_string </b></h3>";
?>

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.

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 "<h3> The original string value is:<b style='color:DarkMagenta'> $string </b></h3>";
    echo "<h3> The substring value is:<b style='color:DarkOrchid'> $sub_string </b></h3>";
?>

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.

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 "<h3> The original string value is:<b style='color:Lime'> $string </b></h3>";
    echo "<h3> The substring value is:<b style='color:LimeGreen'> $sub_string </b></h3>";
?>

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.



from Linux Hint https://ift.tt/3q0SXaU

Post a Comment

0 Comments