Give this a shot:
$url = "http://example.com/list4";
preg_match("#\/(\w+)(\d+)$#", $url, $matches);
echo "String: " . $matches[1] . "<br>";
echo "Number: " . $matches[2] . "<br>";
// print out the URL you wanted above
echo "http://example.com/" . $matches[1] . $matches[2];
EDIT
To fit the needs of the question, you’re going to need to account for .php
at the end of your URL. This is going to work:
$url = $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'];
preg_match("#\/(\w+)(\d+)\.?\w*$#", $url, $matches); // updated
echo "String: " . $matches[1] . "<br>";
echo "Number: " . $matches[2] . "<br>";
// print out the URL you wanted above
echo "http://example.com/" . $matches[1] . $matches[2];
13
solved How to parse url in php to get numbers and strings [closed]