PHP substring is one of the most common PHP string functions. It returns the part of the string. The first argument specifies the string, which will be trimmed.
How to extract characters from a string
To extract a substring from a string in PHP, use the PHP substr() function. The substr() method does not change an original string. If we want lớn extract characters from the end of the string, use a negative start number.
Bạn đang xem: Php
PHP substr
PHP substr() is a built-in string function that returns a part of a string. The substr() method extracts parts of the string, beginning at the character at the specified position, và returns the specified number of characters.
PHP substr() function cuts a part of a string from a string, starting at a specified position. The second argument specifies the position of the trim start. The third argument specifies the length of a string lớn be returned. The start and length arguments can also be negative numbers.
You can grab several characters from a string with a substr() function.
Syntax
The syntax of the PHP String substr() method is the following.
substr(string, start, length)
Arguments
The string parameter is required & specifies the string khổng lồ return a part of.The start parameter is required, & it is the position where lớn start the extraction. The first character is at index 0.
If the start is non-negative, the returned string will start at the start‘th position in the string, counting from zero. So, for instance, in the string ‘abcdef,’ the character at position 0 is ‘a’, the character at position 2 is ‘c,’ and so forth.
If the start is negative, the returned string will start at the start‘th character from the over of the string.
If the string is less than start characters long, FALSE will be returned. In case of failure or the empty string, False is returned.
Xem thêm: Chỉ Số Cung Hoàng Đạo Gia Đình, Xứng Đáng Gửi Gắm Cả Đời, Top Gia Đình Hoàng Đạo Khiến Vạn
The length parameter is optional, và the number of characters to extract. If you omit the string length, it removes the rest of the string.
Example
See the below example.
See the below output.

Use Negative parameters in substr() function
Let’s pass the start as the negative parameter.
See the below output.

If an integer is positive, it refers to lớn start from start_position & extract the length from the beginning. If an integer is negative, it refers lớn start from start_position & remove the length from the kết thúc of the string.
If the length parameter is not passed, then the substr() function will return the string starting from the start_position till the end of the string.
For getting the substring of UTF-8 characters, I highly recommend mb_substr. See the following code.
See the output below.

Conclusion
PHP substr() is one of the most common PHP string functions. It extracts the characters from the string. The first argument specifies the string that will be trimmed. The second argument specifies the position of the trim start. The third argument specifies the length of the string to be returned. The start và length arguments can also be negative numbers.