<?php
$arr = array('/page_name.html',
'/page_name.html/1',
'/page_name.html/2',
'/page_name.html/999',
'/page_name.html14',
);
foreach($arr as $a) {
$matches = array();
preg_match('~\\/(?P<page_name>.*?)\.html(?:\\/(?P<num>\\d+))?$~', $a, $matches);
printf("%s => ",$a);
if ($matches) {
printf(" %s; ", $matches['page_name']);
if (isset($matches['num'])) {
printf("[%d]", $matches['num']);
} else {
echo "No page set";
}
} else {
echo "No matches";
}
echo PHP_EOL;
}
Вывод:
/page_name.html => page_name; No page set
/page_name.html/1 => page_name; [1]
/page_name.html/2 => page_name; [2]
/page_name.html/999 => page_name; [999]
/page_name.html14 => No matches