PHP provides a variety of string interception functions: substr(): intercepts the specified part of the string. substring(): intercept a substring from the end of the string to the beginning. substr_replace(): Replace the specified part of the string. str_replace(): Replace the specified part of the string with other parts.
Function to intercept strings in PHP
PHP provides a variety of functions to intercept strings, among which the most Commonly used ones include:
- substr():Intercept a part of the string (substring) and return it. The syntax is: substr(string $string, int $start, int $length = null), where $start specifies the starting position of the substring, and $length specifies the length of the substring.
For example:
$string = "Hello World!"; $substring = substr($string, 0, 5); // "Hello"
Copy after login
- substring():The function is similar to substr(), but it intercepts sub-characters from the end of the string to the starting position string. The syntax is: substring(string $string, int $start, int $length = null), where $start specifies the starting position of the substring, and $length specifies the length of the substring.
For example:
$string = "Hello World!"; $substring = substring($string, 10, 5); // "World"
Copy after login
- substr_replace():Replace a part of the string with the specified string. The syntax is: substr_replace(string $string, string $replacement, int $start, int $length = null), where $start specifies the starting position of replacement and $length specifies the length of replacement.
For example:
$string = "Hello World!"; $substring = substr_replace($string, "there", 7, 5); // "Hello there!"
Copy after login
- str_replace():Replace some parts of the string with other parts. The syntax is: str_replace(mixed $search, mixed $replace, mixed $subject), where $search specifies the part to be replaced, $replace specifies the replaced part, and $subject specifies the target string.
For example:
$string = "Hello World!"; $substring = str_replace("World", "there", $string); // "Hello there!"
Copy after login
The above is the detailed content of What is the function to intercept string in php. For more information, please follow other related articles on the PHP Chinese website!