- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- 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
Advertisements