What is the function of the “substr” function in PHP?
The substr function is used to extract a part of a string. It can take three parameters.
- String: specified string to extract.
- Starting position: the starting point of the string to be extracted. It can be a positive number (counting from the beginning of the string) or a negative number (counting from the end of the string).
- Length (optional): the length of the string to be extracted. If omitted, all characters from the starting position to the end of the string will be extracted.
The substr function returns a portion of the extracted string as a result. It can be used to extract a portion of a string, extract a specific character position, or obtain a substring of a specified length.