在 PHP 中獲取 URL 的最後一部分?
要獲取 URL 的最後部分,請在 PHP 中使用 preg_match()。假設下面的 URL 為我們的輸入網址
$websiteAddress='https://tutorialspoint.tw/java/java_questions_answers/9989809898';
我們需要獲取以下僅包含最後一部分(一個數字)的輸出
9989809898'
示例
以下是用於獲取 URL 最後一部分的 PHP 程式碼
<!DOCTYPE html>
<html>
<body>
<?php
$websiteAddress='https://tutorialspoint.tw/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
廣告
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP