PHP获取URL后缀名

看到很多面试题都有这个题目,所以实现了一下。

代码:

<?php

// PHP获取URL后缀名

$url = (string) 'https://blog.csdn.net/u012628581/test.php';

try {
    $ext = pathinfo($url, PATHINFO_EXTENSION);
    if (!$ext) {
        throw new \Exception('parse extension error,'.$url);
    }
    echo $ext;
} catch (\Exception $e) {
    echo 'Message:',$e->getMessage(); // log
}123456789101112131415

看网上还有正则或者字符串匹配之类的解决办法,但效率最高的还是系统函数。

猜你喜欢

转载自blog.csdn.net/zl17822307869/article/details/114109793