PHP:未定义的偏移量:3 - preg_match_all

I have URL structure from example:

http://www.example.com/directory/some-text-a1-vs-sec-text-b2-vs-third-text-vs-last-text-c1/

My Regex is:

preg_match_all("/([^\/.]+?)(?:-vs-|\.\w+$)/", $html, $matches);

Expected result:

some-text-a1
sec-text-b2
third-text
last-text-c1

Result I got:

some-text-a1
sec-text-b2
third-text
Notice: Undefined offset: 3 in F:\xampp\htdocs\url.php on line 41

Full code:

$html = "http://www.example.com/directory/some-text-a1-vs-sec-text-b2-vs-third-text-vs-last-text-c1/";
preg_match_all("/([^\/.]+?)(?:-vs-|\.\w+$)/", $html, $matches);

$prvi = "some-text-a1";
$drugi = "sec-text-b2";
$treci = "third-text";
$cetvrti = "last-text-c1";

echo "URL: ".$html."<br>";

if($prvi == $matches[1][0]){echo "1st O.K. - ".$prvi." = ".$matches[1][0]."<br>";}
if($drugi == $matches[1][1]){echo "2nd O.K. - ".$drugi." = ".$matches[1][1]."<br>";}
if($treci == $matches[1][2]){echo "3rd O.K. - ".$treci." = ".$matches[1][2]."<br>";}
if($cetvrti == $matches[1][3]){echo "4th O.K. - ".$cetvrti." = ".$matches[1][3]."<br>";}

Ideas what am I missing? I suppose the ending slash / is the problem within my regex.

Any ideas? Thanks!

Try this

(?<=vs-)(.*?)(?=-vs)|(?<=\/)([^\/]*?)(?=-vs)|(?<=vs-)(.*?)(?=\/|$)

Regex demo

Explanation:
(?<=…): Positive lookbehind sample
( … ): Capturing group sample
.: Any character except line break sample
*: Zero or more times sample
?: Once or none sample
(?=…): Positive lookahead sample
|: Alternation / OR operand sample
\: Escapes a special character sample
[^x]: One character that is not x sample
$: End of string or end of line depending on multiline mode sample

PHP:

<?php
$re = "/(?<=vs-)(.*?)(?=-vs)|(?<=\\/)([^\\/]*?)(?=-vs)|(?<=vs-)(.*?)(?=\\/|$)/"$
$str = "http://www.example.com/directory/some-text-a1-vs-sec-text-b2-vs-third-t$

preg_match_all($re, $str, $matches);
print_r($matches[0]);

Output:

Array
(
    [0] => some-text-a1
    [1] => sec-text-b2
    [2] => third-text
    [3] => last-text-c1
)

This is a different approach - using the parse_url and explode functions.

<?php
$url = 'http://www.example.com/directory/some-text-a1-vs-sec-text-b2-vs-third-text-vs-last-text-c1/';

$parsedUrl = parse_url($url);

var_dump($parsedUrl);

$path = explode('/',trim($parsedUrl['path'],'/'));

var_dump($path);

if (is_array($path) && $path[0] === 'directory') {
        if (isset($path[1])) {
                $vs = explode('-vs-',$path[1]);
                var_dump($vs);
        }
}