How to use substr function to intercept string in PHP

WBOY
Release: 2023-06-26 12:54:02
Original
1130 people have browsed it

In PHP, processing strings is a very common task. Sometimes we need to intercept part of a string, then we can use the substr() function. The substr() function is a powerful string interception function that can help us intercept the required part from a string.

The syntax of the substr function is as follows:

substr(string $string, int $start, int|null $length = null): string|false
Copy after login

Parameter description:

  • $string: required parameter, the string to be intercepted.
  • $start: A required parameter, the starting position of interception, which can be a positive integer or a negative integer.
  • $length: Optional parameter, specifies the intercepted length, which can be a positive integer or a negative integer. If the $length parameter is not passed in, all characters from the $start position to the end of the string will be intercepted.

Return value description:

  • If successful, return the intercepted string.
  • If it fails, return false.

Here are some examples of using the substr function:

  1. Intercept a part of the string

    $name = 'John Doe';
    $first_name = substr($name, 0, 4); // "John"
    $last_name = substr($name, 5, 3); // "Doe"
    Copy after login

    In the above code, $name is the original string, $first_name is the 4 characters intercepted from the starting position 0, and $last_name is the 3 characters intercepted from the 5th character.

  2. Intercept the last few digits of the string

    $file_name = 'example.jpg';
    $file_extension = substr($file_name, -3); // "jpg"
    Copy after login

    In the above code, $file_name is the original string, and $file_extension is intercepted from the third position from the bottom all characters.

  3. Intercept part of the string and convert it to uppercase

    $phrase = 'the quick brown fox jumps over the lazy dog';
    $start = strpos($phrase, 'brown');
    $length = 5;
    $part = strtoupper(substr($phrase, $start, $length)); // "BROWN"
    Copy after login

    In the above code, $phrase is the original string and $start is the string from "brown" is the starting index position, $length is the intercepted length. The intercepted string returned by the substr() function can be converted to uppercase letters by the strtoupper() function.

  4. Intercept multiple strings and merge them

    $phone_number = '555-123-4567';
    $area_code = substr($phone_number, 0, 3); // "555"
    $prefix = substr($phone_number, 4, 3); // "123"
    $suffix = substr($phone_number, 8); // "4567"
    $full_number = $area_code.'-'.$prefix.'-'.$suffix; // "555-123-4567"
    Copy after login

    In the above code, $phone_number is a string containing the complete phone number. First use the substr() function to get the different parts of the phone number and then concatenate them to form the complete phone number string.

    Finally, it should be noted that the substr() function needs to use the mb_substr() function to process Chinese characters. Because Chinese characters occupy multiple bytes in the string, these characters must be segmented and processed correctly.

    I hope that through the introduction of this article, you will be able to use the substr() function more skillfully and easily intercept the required part of the string.

    The above is the detailed content of How to use substr function to intercept string in PHP. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!