- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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
- Related Articles
- How to get parameters from a URL string in PHP?
- MySQL String Last Index Of in a URL?
- URL Decoding in PHP
- How to extract part of a URL in MySQL?
- Get left part of the string on the basis of last occurrence of delimiter in MySQL?
- Find and replace a part of URL records in MySQL?
- PHP – How to get the selected part of a string using mb_substr()?
- How to get current URL in JavaScript?
- How to get current URL in jQuery?
- JavaScript - know the value of GET parameters from URL
- Get the last day of month in Java
- Get last second of a date in MySQL?
- Get last element of each sublist in Python
- How to display the last part of the file in the Linux system?
- Read last line from file in PHP

Advertisements