PHP searches for the current URL File Extension

Source: Internet
Author: User
1 <HTML> 2 <body> 3 <? PHP 4 $ paths [] = $ _ server ['request _ URI ']; // $ _ server ['request _ URI'] Get the current request URI, the domain name 5 $ paths [] =$ _ server ['script _ name']; // $ _ server ['script _ name'] obtains the name of the currently executed script, this path starts from document_root 6 $ paths [] = $ _ server ['script _ filename']; // $ _ server ['script _ filename'] the absolute path name of the currently executed script 7 $ paths [] = $ _ server ['php _ self-service ']; // $ _ server ['php _ Self '] obtains the file name of the script being executed. The path starts from document_root, unlike $ _ server ['script _ name'], if PHP-CGI is used to execute the script, $ _ s Erver ['script _ name'] displays PHP-CGI, while $ _ server ['php _ Self '] displays script files, such as index. PHP 8 $ paths [] = _ file __; // obtain the complete absolute path of the file and the file name 9 10 foreach ($ paths as $ PATH) {11 $ file_names [] = basename ($ PATH); // obtain the file names in the path, all of which are indexes. PHP, the following program uses $ file_names [0]; 12} 13 14 # The first method, using explode15 $ arr = explode ('. ', $ file_names [0]); 16 $ extend_names [] = end ($ ARR); 17 18 # method 2, use strpos and substr19 $ extend_names [] = substr ($ file_names [0], St Rpos ($ file_names [0], '. ') + 1); 20 21 # The third method uses the regular expression 22 $ pattern = '/. + \. (. +)/'; 23 preg_match ($ pattern, $ file_names [0], $ matches); 24 $ extend_names [] = end ($ matches ); 25 26 var_dump ($ paths); 27 echo '<br>'; 28 var_dump ($ file_names); 29 echo '<br>'; 30 var_dump ($ extend_names ); 31 echo '<br>'; 32?> 33 </body> 34 

Access URL: www.local.com/sub/index.php

Page output

Array (5) {[0] => string (14) "/SUB/index. PHP "[1] => string (14)"/SUB/index. PHP "[2] => string (20)" D:/www/SUB/index. PHP "[3] => string (14)"/SUB/index. PHP "[4] => string (20)" D: \ www \ sub \ index. PHP "}
Array (5) {[0] => string (9) "index. PHP "[1] => string (9)" index. PHP "[2] => string (9)" index. PHP "[3] => string (9)" index. PHP "[4] => string (9)" index. PHP "}
Array (3) {[0] => string (3) "php" [1] => string (3) "php" [2] => string (3) "php "}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.