You can also use explode, this give what is immediately after the end of the matched pattern.
function pattern_match($i = 0, $txt = "", $pattern = "")
{
$j = 0;
while($j <= strlen($txt)) {
if ($j >= strlen($pattern)) return true;
if ($pattern[$j] != $txt[$i + $j]) return false;
$j++;
}
}
function scan($txt = "", $pattern = "")
{
$i = 0; $matches = [];
while ($i <= strlen($txt)) {
if(pattern_match($i, $txt, $pattern)) $matches[] = $i + strlen($pattern) - 1;
$i++;
}
return $matches;
}
function get_nth($n = 0, $txt = "", $pattern = "", $j = 1)
{
$indicies = scan($txt, $pattern);
return $indicies[$n] ? substr($txt, $indicies[$n] + 1, $j) : null;
}
$txt = "t|1t|2t|3t|xyz012";
$pattern = "t|";
var_dump(
get_nth(0, $txt, $pattern),
get_nth(1, $txt, $pattern),
get_nth(2, $txt, $pattern),
get_nth(3, $txt, $pattern, 3) // any char to the right
)
// string(1) "1" string(1) "2" string(1) "3" string(3) "xyz"