PHP Substr: How to Extract Characters from String
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 to extract characters from the end of the string, use a negative start number.
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, and 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 to 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 and specifies the string to return a part of.
The start parameter is required, and it is the position where to 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 end 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.
The length parameter is optional, and the number of characters to extract. If you omit the string length, it removes the rest of the string.
Example
See the below example.
<?php // app.php echo substr("AppDividend", 3)."\n"; echo substr("Medium", 2)."\n"; echo substr("LaravelNews", 7)."\n"; echo substr("Laracasts", 4)."\n";
See the below output.
Use Negative parameters in substr() function
Let’s pass the start as the negative parameter.
<?php // app.php echo substr("AppDividend", -3)."\n"; echo substr("Medium", -2)."\n"; echo substr("LaravelNews", -7)."\n"; echo substr("Laracasts",-4)."\n";
See the below output.
If an integer is positive, it refers to start from start_position and extract the length from the beginning. If an integer is negative, it refers to start from start_position and remove the length from the end 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.
<?php // app.php $utf8string = "appdæøå"; echo substr($utf8string,0,5)."\n"; echo mb_substr($utf8string,0,5,'UTF-8');
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 and length arguments can also be negative numbers.
That’s it.