ในการรับส่วนสุดท้ายของ 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