Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> PHP

Lấy phần cuối của URL trong PHP

Để lấy phần cuối URL, dùng preg_match() trong PHP. Giả sử URL đầu vào:

$websiteAddress = 'https://www.tutorialspoint.com/java/java_questions_answers/9989809898';

Cần lấy phần cuối là số:

9989809898

Ví dụ

<!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>

Kết quả

The result is as follows:82345999