- var_dump: documentation ( source)
- preg_match: documentation ( source)
<?php
$expression = "5th from end";
preg_match('~^(?:(?P<offset>[0-9]+)(?:st|nd|rd|th)* from )*(?P<position>start|beginning|last|end)+$~i', $expression, $matches);
var_dump($matches);