Để lấy phần cuối cùng của URL, hãy sử dụng preg_match () trong PHP. Giả sử sau đây là URL đầu vào của chúng tôi
$websiteAddress='https://www.tutorialspoint.com/java/java_questions_answers/9989809898';
Chúng ta cần lấy kết quả sau, chỉ với phần cuối cùng, là một số
9989809898'
Ví dụ
Sau đây là mã PHP để lấy phần cuối cùng của 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> Đầu ra
Điều này sẽ tạo ra kết quả sau
The result is as follows:82345999