Find A smarter method: remove the parts starting with string A and starting with string B.

Source: Internet
Author: User
Remove the parts starting with string A and starting with string B. For example, if {code...} is extracted, the homenews stupid method is: {code...} removes the same part starting with string A and starting with string B.

For example

$a = '/www/proj-asd/app/home/news';$b = '/www/proj-asd/app/_view';

The extracted part ishome/news

The stupid method is:

$a = '/www/proj-asd/app/home/news';$b = '/www/proj-asd/app/_view';echo getRelPath($a, $b); // home/newsfunction getRelPath($a, $b){    $a = strtr($a, '\\', '/');    $b = strtr($b, '\\', '/');    $a = explode('/', $a);    $b = explode('/', $b);    foreach ($a as $i=>$av) {        $bv = $b[$i];        if ($av===$bv) {            unset($a[$i]);        }else{            break;        }    }    return implode('/', $a);}

Reply content:

Remove the parts starting with string A and starting with string B.

For example

$a = '/www/proj-asd/app/home/news';$b = '/www/proj-asd/app/_view';

The extracted part ishome/news

The stupid method is:

$a = '/www/proj-asd/app/home/news';$b = '/www/proj-asd/app/_view';echo getRelPath($a, $b); // home/newsfunction getRelPath($a, $b){    $a = strtr($a, '\\', '/');    $b = strtr($b, '\\', '/');    $a = explode('/', $a);    $b = explode('/', $b);    foreach ($a as $i=>$av) {        $bv = $b[$i];        if ($av===$bv) {            unset($a[$i]);        }else{            break;        }    }    return implode('/', $a);}

First, the question is interpreted as Path Matching (rather than pure string)

Soarray_diff_assocIt may be the method that the subject wants.

Update and solve the bug mentioned by @ AlanZhang

php
  

http://3v4l.org/eB8Km

It is required that all characters be matched only when each character is matched.

for(var index=0;index<$a.length;index++){    if($a.charAt(index)!=$b.charAt(index)){       return $a.substring(index);    }}

Sit down and try again.

For ($ I = 0; $ I
   

Isn't callback faster? If the address is determined, convert it to an array and then use the callback function. The idea is similar and you will not write it.

$a = '/www/proj-asd/app/home/news';$b = '/www/proj-asd/app/_view';function getRelPath($a, $b){    if($a[0] != $b[0]) return $a;    return getRelPath(substr($a, 1), substr($b, 1));}var_dump(getRelPath($a, $b));

The jsven method is the same as that of the main user.
Yan Shijie's method is actually the same as that of the poster, but it is a recursive version.

Here is an idea to take the minimum length of a B.
Then perform a binary classification.

$ A = '/www/proj-asd/app/home/News ';
$ B = '/www/proj-asd/app/_ view ';

Half cut, the same as/www/proj-.

The remaining part is 'sd/app/_ view', followed by half,
Is sd/app/, the same. In this case, the same part is/www/proj-asd/app/

The remaining values are 'home/'and'View 'comparison.
Cut half, ho and _ v, different.
Half more, h and
, Different.

So the same part is/www/proj-asd/app/

However, when the code is written, it is certainly not easy to compare characters directly.

If you want to edit a file list, such as removing the version library directory from the file list in the SVN log, you can directly use column Editing:
Gg-Move to the beginning
Ctrl + v-start column Mode
W or l-move the cursor to the right to the desired position
...
X-delete a public prefix

After skill, it is much faster than Ctrl + h.

It can be replaced with the string location search function, but shell is recommended.

Simply replace a regular expression with null ....

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.