In PHP, the tool used to find the relative path or absolute path of a file has a previous interview question titled Finding the relative path of two files. at that time, I felt that there was no computer, and it was too troublesome to write the code. I didn't do that question, today's work is relatively idle, and I think of it. I have completed two functions. the code has not been tested in the actual production environment. if you reference it to the production environment, please read it carefully, mainly refer to learning to view the source code printing help 1 & lt ;? PHP tool for searching relative or absolute paths of files
In the previous interview, I wrote a document entitled "finding the relative path of two files". at that time, I felt that there was no computer, and the code was too troublesome to write. I didn't do that question. I was busy working today, so I remembered this, two functions have been completed, and the code has not been tested in the actual production environment. if you reference it to the production environment, please read it carefully, mainly for reference and learning.
View source code printing help
1
6 * @ version $ Id: v 1.0, * @ copyright (c) Copyright; tsingyuan, 20138 */9 10/** 11 * the absolute path of the returned File 12*13 * @ param string $ filename14 * @ return string15 */16 function absoluteroute ($ filename) 17 {18 $ split = '/\/| \\\/'; 19 $ currentdir = preg_split ($ split, dirname (_ FILE __)); 20 $ dirarr = preg_split ($ split, $ filename); 21 $ diracount = count ($ dirarr); 22 $ cda = count ($ currentdir); 23 if (strpos (PH P_ OS, 'win ')! = False) 24 $ reg = '/\ w \:/'; 25 else26 $ reg = '//'; 27 if (! Preg_match ($ reg, $ dirarr [0]) 28 {29 foreach ($ dirarr as $ nk => $ name) 30 {31 if ($ name = ". "| $ name = '.. ') 32 {33 if ($ name = '.. ') 34 $ filenamearr = array_slice ($ currentdir, 0,-($ nk + 1); 35 if ($ name = '. '& $ nk = '0') 36 $ filenamearr = array_slice ($ currentdir, 0); 37} 38 else39 {40 $ filenamearr [] = $ name; 41} 42} 43 $ filename = implode ('/', $ filenamearr); 44} 45 return $ filename; 46} 47 48/** 49 * return two objects Relative path of the component (PS: ^ _ ^ good php learning exchange group: 276167802, verification: csl, if you are interested, join in for discussion) 50 * (to ensure the input relative path parameters are available, the function relies on absoluteroute) 51*52 * @ param string $ filenamea53 * @ param string $ filenameb54 * @ return string55 */56 function relativeroute ($ filenamea, $ filenameb) 57 {58 $ split = '// |\\\/'; 59 $ filenamea = absoluteroute ($ filenamea); 60 $ filenameb = absoluteroute ($ filenameb ); 61 $ dira = preg_split ($ split, $ filenamea); 62 $ dirb = preg _ Split ($ split, $ filenameb); 63 $ flag = true; 64 if (count ($ dira)> = count ($ dirb )) 65 {66 $ tmp = array (); 67 $ tmp = $ dira; 68 $ dira = $ dirb; 69 $ dirb = $ tmp; 70 $ flag = false; 71} 72 foreach ($ dira as $ k => $ v) 73 if ($ v! = $ Dirb [$ k]) break; 74 75 $ dirr = array_slice ($ dirb, $ k); 76 77 $ k = 1? $ Dtag = '/': $ dtag = './'; 78 $ result = $ dtag. implode ('/', $ dirr); 79 if (! $ Flag) 80 {81 if ($ dira [0] = '') array_shift ($ dira); 82 foreach ($ dirr as $ kk => & $ v) 83 ($ kk + 1 )! = Count ($ dirr )? 84 ($ v? 85 $ v = '.. ': 86 $ v = '. '87): 88 $ v = implode ('/', array_slice ($ dira, $ k); 89 $ result = $ dtag. implode ('/', $ dirr); 90} 91 return $ result; 92}
This article is from the "Qingyuan education" blog. For more information, see here. thank you!