php editor Banana will introduce you in detail how to use PHP's substr() function to return part of a string. The substr() function can intercept a substring at a specified position and length in a string, which is very practical. Through the explanation of this article, you will understand the specific usage and parameter meaning of the substr() function, and easily realize the partial interception function of strings. Read now to master the skills of string processing in PHP!
PHP string interception
Introduction
php Provides a variety of methods to intercept part of a string to meet different needs. The following are commonly used string interception functions:
substr()
substr()
The function gets the substring at the specified position in the string. The syntax is as follows:
substr(string $string, int $start, int $length)
$string
: The string to be intercepted$start
: The starting position of the substring (starting from 0)$length
: The length of the substringFor example:
$string = "Hello world"; $substring = substr($string, 6, 5); // Result: world
substring()
substring()
Function is similar to substr()
, but it does not accept negative starting positions. The syntax is as follows:
substring(string $string, int $start, int $end)
$string
: The string to be intercepted$start
: The starting position of the substring (starting from 0)$end
: The end position of the substring (starting from 1)For example:
$string = "Hello world"; $substring = substring($string, 6, 11); // Result: world
str_split()
str_split()
The function splits a string into an array, each element containing one character. The syntax is as follows:
str_split(string $string, int $length)
$string
: The string to be split$length
: The length of each element (optional, default is 1)For example:
$string = "Hello world"; $array = str_split($string); // Result: ["H", "e", "l", "l", "o", " ", "w", "o", "r", " l", "d"]
slice()
slice()
The function is a new feature introduced in PHP 8, which provides a more concise way to intercept strings. The syntax is as follows:
slice(string $string, int $start, int $end)
$string
: The string to be intercepted$start
: The starting position of the substring (starting from 0)$end
: The end position of the substring (starting from 1)slice()
The function does not modify the original string, but creates a new substring in memory. For example:
$string = "Hello world"; $substring = $string->slice(6, 11); // Result: world
Regular expression
Regular expressions can also be used to intercept part of a string. By using capturing groups, you can match and extract specific parts of a string. For example:
$string = "Hello world 123"; $pattern = "/world (d )/"; preg_match($pattern, $string, $matches); $substring = $matches[1]; // Result: 123
other options
In addition to the above functions, there are some other options that can be used to intercept part of a string:
chop()
: Remove spaces and newlines at the end of the stringtrim()
: Remove spaces and newlines at both ends of the stringexplode()
: Split the string into an array based on the delimiterimplode()
: Concatenate the array into a stringThe above is the detailed content of How to return part of a string in PHP. For more information, please follow other related articles on the PHP Chinese website!