Computer >> Computer tutorials >  >> Programming >> PHP

Get Last Part of URL in PHP?


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 

<!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