I saw that many interview questions have this topic, so I realized it.
Code:
<?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
Look at the Internet and there are solutions such as regular or string matching, but the most efficient is the system function.