Get Last Part of URL in PHP?

PHPServer Side ProgrammingProgramming

To get the last part of URL, use preg_match() in PHP. Let’s say the following is our input URL 

$websiteAddress='https://www.tutorialspoint.com/java/java_questions_answers/9989809898';

We need to get the following output, with only the last part, which is a number 

9989809898'

Example

Following is the PHP code to get the last part of URL 

 Live Demo

<!DOCTYPE html>
<html>
<body>
<?php
$websiteAddress='https://www.tutorialspoint.com/java/java_questions_answers/9989809898';
if(preg_match("/\/(\d+)$/",$websiteAddress,$recordMatch)){
   $lastResult=$recordMatch[1];
}
echo "The result is as follows:",$lastResult;
?>
</body>
</html>

Output

This will produce the following output

The result is as follows:82345999
raja
Published on 13-Oct-2020 07:39:48
Advertisements