Php calculates the implementation code of the relative paths of two files
How to calculate the relative path between two files? Php implementation is very simple. Here we share a piece of code to calculate the relative paths of the two files. if you need it, please refer to it. Calculate the relative path of the two files. For example, a file is as follows: $ a = "/a/B/c/d/e. php"; $ B = "/a/B/12/34/c. php ". How can we calculate the relative path of B to? Code:
$ Counta) {while ($ countb> $ counta) {$ path. = ".. /"; $ countb -- ;}/// find the first public node for ($ I = $ countb-1; $ I >=0 ;) {if ($ patha [$ I]! = $ Pathb [$ I]) {$ path. = ".. /"; $ I --;} else {// determine whether it is the real first public node to prevent the occurrence of sub-directory name duplication. for ($ j = $ I-1, $ flag = 1; $ j> = 0; $ j --) {if ($ patha [$ j] = $ pathb [$ j]) {continue ;} else {$ flag = 0; break ;}} if ($ flag) break; else $ I ++ ;}}for ($ I + = 1; $ I <= $ counta; $ I ++) {$ path. = $ patha [$ I]. "/";} return $ path;} // call example $ a = "/a/c/d/e. php "; $ B ="/a/c. php "; $ path = getRelativePath ($ a, $ B); echo $ path; ?> |