Removing characters from a string is most common and frequent activities in PHP. For example, you have a long name, but your system does not allow to show more than 6 characters of your name. In this case, you need to keep only first 6 characters from the name. To solve this issue we use substr() function.
– Solution to remove character from a string:
The substr() function permits you to slice a string into smaller strings. It typically accepts three arguments, but last argument is optional. First argument is your input string(example: your name), second arguments is the position to begin slicing, and last one is the number of characters to return from its start position.
A negative value for the third argument tells PHP to remove characters from the end of the string. Use the substr() function to slice off the required number of characters from the beginning or end of the string. Have a look following snippet-
// define string $str = "howtocheck"; // remove first 6 characters // result: "heck" $first_substring = substr($str, 6); echo $first_substring; // remove last 6 characters // result: "howt" $last_substring = substr($str, 0, -6); echo $last_substring;
Enjoy! Was this information useful? What other tips would you like to read about in the future? Share your comments, feedback and experiences with us by commenting below!