Php calculates the relative paths of two files. _ PHP Tutorial

Source: Internet
Author: User
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...

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.