在 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

更新於:13-Oct-2020

2K+ 瀏覽量

啟動你的 職業生涯

完成課程並獲得認證

開始學習
廣告