เพื่อรับส่วนสุดท้ายของ URL ใช้ preg_match() ใน PHP สมมติ URL อินพุตของเราคือ
$websiteAddress='https://www.tutorialspoint.com/java/java_questions_answers/9989809898';
เราต้องการรับเอาต์พุตดังนี้ เพียงส่วนสุดท้ายซึ่งเป็นตัวเลข
9989809898
ตัวอย่าง
โค้ด PHP ต่อไปนี้เพื่อรับส่วนสุดท้ายของ 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>
ผลลัพธ์
นี่จะผลิตเอาต์พุตดังนี้
The result is as follows:82345999