Php calculates the relative paths of two files ,. Php calculates the relative paths of two files. This article describes how php calculates the relative paths of the two files. Share it with you for your reference. Details: 1. problem: write a php method to calculate the relative paths of two files,
This article describes how to calculate the relative paths of two files in php. Share it with you for your reference. The details are as follows:
I. problems:
Write a php function to calculate the relative path of the two files. For example, $ a = "/a/B/c/d/e. php "; $ B ="/a/B/12/34/c. php ", what is the relative path of B to?
II. solution:
<? Php/*** calculates the relative path of $ B relative to $ a * @ param string $ B * @ return string */function getRelativePath ($, $ B) {$ patha = explode ('/', $ a); $ pathb = explode ('/', $ B); $ counta = count ($ patha) -1; $ countb = count ($ pathb)-1; $ path = ".. /"; if ($ countb> $ 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;} $ a = "/a/c/d/e. php "; $ B ="/a/c. php "; $ path = getRelativePath ($ a, $ B); echo $ path;
I hope this article will help you with php programming.
Example: This article describes how php calculates the relative paths of two files. Share it with you for your reference. Details: 1. question: write 1...