Hướng dẫn how to get data between two string in php? - làm thế nào để lấy dữ liệu giữa hai chuỗi trong php?

Phần lớn các câu trả lời ở đây không trả lời phần đã được chỉnh sửa, tôi đoán chúng đã được thêm vào trước đó. Nó có thể được thực hiện với Regex, như một câu trả lời đề cập. Tôi đã có một cách tiếp cận khác.

Hàm này tìm kiếm $ String và tìm chuỗi đầu tiên giữa các chuỗi $ bắt đầu và $ end, bắt đầu ở vị trí bù $. Sau đó, nó cập nhật vị trí bù $ để chỉ vào đầu kết quả. Nếu $ bao gồm các nhà là đúng, nó bao gồm các dấu phân cách trong kết quả.first string between $start and $end strings, starting at $offset position. It then updates the $offset position to point to the start of the result. If $includeDelimiters is true, it includes the delimiters in the result.

Nếu không tìm thấy chuỗi $ start hoặc $ end, nó sẽ trả về null. Nó cũng trả về null nếu $ chuỗi, $ start hoặc $ end là một chuỗi trống.

function str_between[string $string, string $start, string $end, bool $includeDelimiters = false, int &$offset = 0]: ?string
{
    if [$string === '' || $start === '' || $end === ''] return null;

    $startLength = strlen[$start];
    $endLength = strlen[$end];

    $startPos = strpos[$string, $start, $offset];
    if [$startPos === false] return null;

    $endPos = strpos[$string, $end, $startPos + $startLength];
    if [$endPos === false] return null;

    $length = $endPos - $startPos + [$includeDelimiters ? $endLength : -$startLength];
    if [!$length] return '';

    $offset = $startPos + [$includeDelimiters ? 0 : $startLength];

    $result = substr[$string, $offset, $length];

    return [$result !== false ? $result : null];
}

Hàm sau đây tìm thấy tất cả các chuỗi nằm giữa hai chuỗi [không có sự chồng chéo]. Nó yêu cầu chức năng trước đó và các đối số là như nhau. Sau khi thực hiện, $ Offset chỉ vào đầu chuỗi kết quả được tìm thấy cuối cùng.all strings that are between two strings [no overlaps]. It requires the previous function, and the arguments are the same. After execution, $offset points to the start of the last found result string.

function str_between_all[string $string, string $start, string $end, bool $includeDelimiters = false, int &$offset = 0]: ?array
{
    $strings = [];
    $length = strlen[$string];

    while [$offset < $length]
    {
        $found = str_between[$string, $start, $end, $includeDelimiters, $offset];
        if [$found === null] break;

        $strings[] = $found;
        $offset += strlen[$includeDelimiters ? $found : $start . $found . $end]; // move offset to the end of the newfound string
    }

    return $strings;
}

Examples:

str_between_all['foo 1 bar 2 foo 3 bar', 'foo', 'bar'] cho [' 1 ', ' 3 '].

str_between_all['foo 1 bar 2', 'foo', 'bar'] cho [' 1 '].

function str_between_all[string $string, string $start, string $end, bool $includeDelimiters = false, int &$offset = 0]: ?array
{
    $strings = [];
    $length = strlen[$string];

    while [$offset < $length]
    {
        $found = str_between[$string, $start, $end, $includeDelimiters, $offset];
        if [$found === null] break;

        $strings[] = $found;
        $offset += strlen[$includeDelimiters ? $found : $start . $found . $end]; // move offset to the end of the newfound string
    }

    return $strings;
}
0 cho [' 1 ', ' 3 '].

function str_between_all[string $string, string $start, string $end, bool $includeDelimiters = false, int &$offset = 0]: ?array
{
    $strings = [];
    $length = strlen[$string];

    while [$offset < $length]
    {
        $found = str_between[$string, $start, $end, $includeDelimiters, $offset];
        if [$found === null] break;

        $strings[] = $found;
        $offset += strlen[$includeDelimiters ? $found : $start . $found . $end]; // move offset to the end of the newfound string
    }

    return $strings;
}
2 cho
function str_between_all[string $string, string $start, string $end, bool $includeDelimiters = false, int &$offset = 0]: ?array
{
    $strings = [];
    $length = strlen[$string];

    while [$offset < $length]
    {
        $found = str_between[$string, $start, $end, $includeDelimiters, $offset];
        if [$found === null] break;

        $strings[] = $found;
        $offset += strlen[$includeDelimiters ? $found : $start . $found . $end]; // move offset to the end of the newfound string
    }

    return $strings;
}
3.

Xem thảo luận

Cải thiện bài viết

Lưu bài viết

  • Đọc
  • Bàn luận
  • Xem thảo luận

    Cải thiện bài viết

    Lưu bài viết

    Đọc

    Bàn luậnsubstr[] Method: The substr[] method is used to retrieve a substring of the original string. It takes as indexes the start and end indexes and extracts the part of the string lying between the indexes. In order to retrieve the substring from the beginning, the start-index is chosen to be 0. 

    substr[string, startIndex, lengthStr]

    Parameters: 

    • Một chuỗi là một chuỗi các ký tự được lưu trữ ở dạng gia tăng trong PHP. Một tập hợp các ký tự, một hoặc nhiều có thể nằm giữa hai chỉ mục được chọn của chuỗi. Văn bản giữa hai ký tự trong PHP có thể được trích xuất bằng hai phương pháp sau: & nbsp; In this parameter, we pass the original string or the string that needs to be cut or modified. It is a mandatory parameter.
    • Cách tiếp cận 1: Sử dụng phương thức Subr []: Phương thức Subr [] được sử dụng để truy xuất một chuỗi con của chuỗi gốc. Nó lấy chỉ mục các chỉ mục bắt đầu và kết thúc và trích xuất một phần của chuỗi nằm giữa các chỉ mục. Để truy xuất chuỗi con từ đầu, chỉ số bắt đầu được chọn là 0. & nbsp; It refers to the position of the original string from where the part needs to be extracted. In this, we pass an integer. If the integer is positive it refers to the start of the position in the string from the beginning. If the integer is negative then it refers to the start of the position from the end of the string. This is also a mandatory parameter.
    • Chuỗi: Trong tham số này, chúng tôi vượt qua chuỗi gốc hoặc chuỗi cần được cắt hoặc sửa đổi. Đó là một tham số bắt buộc. It is an optional parameter of integer type. It refers to the length of the part of the string that needs to be cut from the original string. If the integer is positive, it refers to start from start_position and extract length from the beginning. If the integer is negative then it refers to start from start_position and extract the length from the end of the string. If this parameter is not passed, then the substr[] function will return the string starting from start_position till the end of the string.

    PHP

    function str_between_all[string $string, string $start, string $end, bool $includeDelimiters = false, int &$offset = 0]: ?array
    {
        $strings = [];
        $length = strlen[$string];
    
        while [$offset < $length]
        {
            $found = str_between[$string, $start, $end, $includeDelimiters, $offset];
            if [$found === null] break;
    
            $strings[] = $found;
            $offset += strlen[$includeDelimiters ? $found : $start . $found . $end]; // move offset to the end of the newfound string
        }
    
        return $strings;
    }
    
    4

    function str_between_all[string $string, string $start, string $end, bool $includeDelimiters = false, int &$offset = 0]: ?array
    {
        $strings = [];
        $length = strlen[$string];
    
        while [$offset < $length]
        {
            $found = str_between[$string, $start, $end, $includeDelimiters, $offset];
            if [$found === null] break;
    
            $strings[] = $found;
            $offset += strlen[$includeDelimiters ? $found : $start . $found . $end]; // move offset to the end of the newfound string
        }
    
        return $strings;
    }
    
    5
    function str_between_all[string $string, string $start, string $end, bool $includeDelimiters = false, int &$offset = 0]: ?array
    {
        $strings = [];
        $length = strlen[$string];
    
        while [$offset < $length]
        {
            $found = str_between[$string, $start, $end, $includeDelimiters, $offset];
            if [$found === null] break;
    
            $strings[] = $found;
            $offset += strlen[$includeDelimiters ? $found : $start . $found . $end]; // move offset to the end of the newfound string
        }
    
        return $strings;
    }
    
    6
    function str_between_all[string $string, string $start, string $end, bool $includeDelimiters = false, int &$offset = 0]: ?array
    {
        $strings = [];
        $length = strlen[$string];
    
        while [$offset < $length]
        {
            $found = str_between[$string, $start, $end, $includeDelimiters, $offset];
            if [$found === null] break;
    
            $strings[] = $found;
            $offset += strlen[$includeDelimiters ? $found : $start . $found . $end]; // move offset to the end of the newfound string
        }
    
        return $strings;
    }
    
    7
    function str_between_all[string $string, string $start, string $end, bool $includeDelimiters = false, int &$offset = 0]: ?array
    {
        $strings = [];
        $length = strlen[$string];
    
        while [$offset < $length]
        {
            $found = str_between[$string, $start, $end, $includeDelimiters, $offset];
            if [$found === null] break;
    
            $strings[] = $found;
            $offset += strlen[$includeDelimiters ? $found : $start . $found . $end]; // move offset to the end of the newfound string
        }
    
        return $strings;
    }
    
    8

    function str_between_all[string $string, string $start, string $end, bool $includeDelimiters = false, int &$offset = 0]: ?array
    {
        $strings = [];
        $length = strlen[$string];
    
        while [$offset < $length]
        {
            $found = str_between[$string, $start, $end, $includeDelimiters, $offset];
            if [$found === null] break;
    
            $strings[] = $found;
            $offset += strlen[$includeDelimiters ? $found : $start . $found . $end]; // move offset to the end of the newfound string
        }
    
        return $strings;
    }
    
    9
    substr[string, startIndex, lengthStr]
    0
    substr[string, startIndex, lengthStr]
    1
    substr[string, startIndex, lengthStr]
    2

    function str_between_all[string $string, string $start, string $end, bool $includeDelimiters = false, int &$offset = 0]: ?array
    {
        $strings = [];
        $length = strlen[$string];
    
        while [$offset < $length]
        {
            $found = str_between[$string, $start, $end, $includeDelimiters, $offset];
            if [$found === null] break;
    
            $strings[] = $found;
            $offset += strlen[$includeDelimiters ? $found : $start . $found . $end]; // move offset to the end of the newfound string
        }
    
        return $strings;
    }
    
    9
    substr[string, startIndex, lengthStr]
    0
    function str_between_all[string $string, string $start, string $end, bool $includeDelimiters = false, int &$offset = 0]: ?array
    {
        $strings = [];
        $length = strlen[$string];
    
        while [$offset < $length]
        {
            $found = str_between[$string, $start, $end, $includeDelimiters, $offset];
            if [$found === null] break;
    
            $strings[] = $found;
            $offset += strlen[$includeDelimiters ? $found : $start . $found . $end]; // move offset to the end of the newfound string
        }
    
        return $strings;
    }
    
    5
    substr[string, startIndex, lengthStr]
    6
    substr[string, startIndex, lengthStr]
    7
    substr[string, startIndex, lengthStr]
    2

    Original String : Hi! This is geeksforgeeks
    Modified String : his 
    9 str_between_all['foo 1 bar 2 foo 3 bar', 'foo', 'bar']0

    str_between_all['foo 1 bar 2 foo 3 bar', 'foo', 'bar']1 str_between_all['foo 1 bar 2 foo 3 bar', 'foo', 'bar']2

    function str_between_all[string $string, string $start, string $end, bool $includeDelimiters = false, int &$offset = 0]: ?array
    {
        $strings = [];
        $length = strlen[$string];
    
        while [$offset < $length]
        {
            $found = str_between[$string, $start, $end, $includeDelimiters, $offset];
            if [$found === null] break;
    
            $strings[] = $found;
            $offset += strlen[$includeDelimiters ? $found : $start . $found . $end]; // move offset to the end of the newfound string
        }
    
        return $strings;
    }
    
    9
    substr[string, startIndex, lengthStr]
    0
    substr[string, startIndex, lengthStr]
    9
    substr[string, startIndex, lengthStr]
    2

    array_split[ string ]
    3

    Đầu ra

    Original String : Hi! This is geeksforgeeks
    Modified String : This is

    Xem thảo luậnstr_split[] Method: The str_split[] method is used to split the specified string into an array, where the elements are mapped to their corresponding indexes. The indexes of the array begin with 0. 

    array_split[ string ]

    Một vòng lặp vòng được thực hiện trên chiều dài mảng. Mỗi khi lần lặp được thực hiện, chỉ mục được kiểm tra nếu nó nằm giữa chỉ mục bắt đầu và kết thúc. Nếu nó nằm trong phạm vi, nhân vật được trích xuất. & nbsp;

    PHP

    function str_between_all[string $string, string $start, string $end, bool $includeDelimiters = false, int &$offset = 0]: ?array
    {
        $strings = [];
        $length = strlen[$string];
    
        while [$offset < $length]
        {
            $found = str_between[$string, $start, $end, $includeDelimiters, $offset];
            if [$found === null] break;
    
            $strings[] = $found;
            $offset += strlen[$includeDelimiters ? $found : $start . $found . $end]; // move offset to the end of the newfound string
        }
    
        return $strings;
    }
    
    4

    function str_between_all[string $string, string $start, string $end, bool $includeDelimiters = false, int &$offset = 0]: ?array
    {
        $strings = [];
        $length = strlen[$string];
    
        while [$offset < $length]
        {
            $found = str_between[$string, $start, $end, $includeDelimiters, $offset];
            if [$found === null] break;
    
            $strings[] = $found;
            $offset += strlen[$includeDelimiters ? $found : $start . $found . $end]; // move offset to the end of the newfound string
        }
    
        return $strings;
    }
    
    5
    function str_between_all[string $string, string $start, string $end, bool $includeDelimiters = false, int &$offset = 0]: ?array
    {
        $strings = [];
        $length = strlen[$string];
    
        while [$offset < $length]
        {
            $found = str_between[$string, $start, $end, $includeDelimiters, $offset];
            if [$found === null] break;
    
            $strings[] = $found;
            $offset += strlen[$includeDelimiters ? $found : $start . $found . $end]; // move offset to the end of the newfound string
        }
    
        return $strings;
    }
    
    6
    function str_between_all[string $string, string $start, string $end, bool $includeDelimiters = false, int &$offset = 0]: ?array
    {
        $strings = [];
        $length = strlen[$string];
    
        while [$offset < $length]
        {
            $found = str_between[$string, $start, $end, $includeDelimiters, $offset];
            if [$found === null] break;
    
            $strings[] = $found;
            $offset += strlen[$includeDelimiters ? $found : $start . $found . $end]; // move offset to the end of the newfound string
        }
    
        return $strings;
    }
    
    7
    function str_between_all[string $string, string $start, string $end, bool $includeDelimiters = false, int &$offset = 0]: ?array
    {
        $strings = [];
        $length = strlen[$string];
    
        while [$offset < $length]
        {
            $found = str_between[$string, $start, $end, $includeDelimiters, $offset];
            if [$found === null] break;
    
            $strings[] = $found;
            $offset += strlen[$includeDelimiters ? $found : $start . $found . $end]; // move offset to the end of the newfound string
        }
    
        return $strings;
    }
    
    8

    function str_between_all[string $string, string $start, string $end, bool $includeDelimiters = false, int &$offset = 0]: ?array
    {
        $strings = [];
        $length = strlen[$string];
    
        while [$offset < $length]
        {
            $found = str_between[$string, $start, $end, $includeDelimiters, $offset];
            if [$found === null] break;
    
            $strings[] = $found;
            $offset += strlen[$includeDelimiters ? $found : $start . $found . $end]; // move offset to the end of the newfound string
        }
    
        return $strings;
    }
    
    9
    substr[string, startIndex, lengthStr]
    0
    substr[string, startIndex, lengthStr]
    1
    substr[string, startIndex, lengthStr]
    2

    function str_between_all[string $string, string $start, string $end, bool $includeDelimiters = false, int &$offset = 0]: ?array
    {
        $strings = [];
        $length = strlen[$string];
    
        while [$offset < $length]
        {
            $found = str_between[$string, $start, $end, $includeDelimiters, $offset];
            if [$found === null] break;
    
            $strings[] = $found;
            $offset += strlen[$includeDelimiters ? $found : $start . $found . $end]; // move offset to the end of the newfound string
        }
    
        return $strings;
    }
    
    9
    substr[string, startIndex, lengthStr]
    0
    function str_between_all[string $string, string $start, string $end, bool $includeDelimiters = false, int &$offset = 0]: ?array
    {
        $strings = [];
        $length = strlen[$string];
    
        while [$offset < $length]
        {
            $found = str_between[$string, $start, $end, $includeDelimiters, $offset];
            if [$found === null] break;
    
            $strings[] = $found;
            $offset += strlen[$includeDelimiters ? $found : $start . $found . $end]; // move offset to the end of the newfound string
        }
    
        return $strings;
    }
    
    5
    substr[string, startIndex, lengthStr]
    6
    substr[string, startIndex, lengthStr]
    7
    substr[string, startIndex, lengthStr]
    2

    Original String : Hi! This is geeksforgeeks
    Modified String : his 
    9 str_between_all['foo 1 bar 2 foo 3 bar', 'foo', 'bar']0

    str_between_all['foo 1 bar 2 foo 3 bar', 'foo', 'bar']1 str_between_all['foo 1 bar 2 foo 3 bar', 'foo', 'bar']2

    str_between_all['foo 1 bar 2 foo 3 bar', 'foo', 'bar']3

    function str_between_all[string $string, string $start, string $end, bool $includeDelimiters = false, int &$offset = 0]: ?array
    {
        $strings = [];
        $length = strlen[$string];
    
        while [$offset < $length]
        {
            $found = str_between[$string, $start, $end, $includeDelimiters, $offset];
            if [$found === null] break;
    
            $strings[] = $found;
            $offset += strlen[$includeDelimiters ? $found : $start . $found . $end]; // move offset to the end of the newfound string
        }
    
        return $strings;
    }
    
    6str_between_all['foo 1 bar 2 foo 3 bar', 'foo', 'bar']5
    substr[string, startIndex, lengthStr]
    0__15
    substr[string, startIndex, lengthStr]
    2

    function str_between_all[string $string, string $start, string $end, bool $includeDelimiters = false, int &$offset = 0]: ?array
    {
        $strings = [];
        $length = strlen[$string];
    
        while [$offset < $length]
        {
            $found = str_between[$string, $start, $end, $includeDelimiters, $offset];
            if [$found === null] break;
    
            $strings[] = $found;
            $offset += strlen[$includeDelimiters ? $found : $start . $found . $end]; // move offset to the end of the newfound string
        }
    
        return $strings;
    }
    
    9
    substr[string, startIndex, lengthStr]
    0
    Original String : Hi! This is geeksforgeeks
    Modified String : This is
    7
    substr[string, startIndex, lengthStr]
    2

    [' 1 ', ' 3 ']3

    substr[string, startIndex, lengthStr]
    0[' 1 ', ' 3 ']5 [' 1 ', ' 3 ']6[' 1 ', ' 3 ']5 [' 1 ', ' 3 ']8str_between_all['foo 1 bar 2 foo 3 bar', 'foo', 'bar']3str_between_all['foo 1 bar 2', 'foo', 'bar']0[' 1 ', ' 3 ']55____82

    str_between_all['foo 1 bar 2', 'foo', 'bar']3str_between_all['foo 1 bar 2', 'foo', 'bar']4___

    [' 1 ']4[' 1 ']5

    substr[string, startIndex, lengthStr]
    0str_between_all['foo 1 bar 2 foo 3 bar', 'foo', 'bar']3[' 1 ']8[' 1 ', ' 3 ']5
    function str_between_all[string $string, string $start, string $end, bool $includeDelimiters = false, int &$offset = 0]: ?array
    {
        $strings = [];
        $length = strlen[$string];
    
        while [$offset < $length]
        {
            $found = str_between[$string, $start, $end, $includeDelimiters, $offset];
            if [$found === null] break;
    
            $strings[] = $found;
            $offset += strlen[$includeDelimiters ? $found : $start . $found . $end]; // move offset to the end of the newfound string
        }
    
        return $strings;
    }
    
    00

    str_between_all['foo 1 bar 2', 'foo', 'bar']3

    function str_between_all[string $string, string $start, string $end, bool $includeDelimiters = false, int &$offset = 0]: ?array
    {
        $strings = [];
        $length = strlen[$string];
    
        while [$offset < $length]
        {
            $found = str_between[$string, $start, $end, $includeDelimiters, $offset];
            if [$found === null] break;
    
            $strings[] = $found;
            $offset += strlen[$includeDelimiters ? $found : $start . $found . $end]; // move offset to the end of the newfound string
        }
    
        return $strings;
    }
    
    02

    function str_between_all[string $string, string $start, string $end, bool $includeDelimiters = false, int &$offset = 0]: ?array
    {
        $strings = [];
        $length = strlen[$string];
    
        while [$offset < $length]
        {
            $found = str_between[$string, $start, $end, $includeDelimiters, $offset];
            if [$found === null] break;
    
            $strings[] = $found;
            $offset += strlen[$includeDelimiters ? $found : $start . $found . $end]; // move offset to the end of the newfound string
        }
    
        return $strings;
    }
    
    02

    array_split[ string ]
    3

    Đầu ra

    Original String : Hi! This is geeksforgeeks
    Modified String : his 


    Bài Viết Liên Quan

    Chủ Đề