I'm looking for a simple way to find matching portions of two strings in PHP (specifically in the context of a URI)

For example, consider the two strings:

http://2.2.2.2/~machinehost/deployment_folder/

and

/~machinehost/deployment_folder/users/bob/settings

What I need is to chop off the matching portion of these two strings from the second string, resulting in:

users/bob/settings

before appending the first string as a prefix, forming an absolute URI.

Is there some simple way (in PHP) to compare two arbitrary strings for matching substrings within them?

EDIT: as pointed out, I meant the longest matching substring common to both strings

Comments

What are the criteria here? Because technically, the h in "http" will match the h in "machinehost". You're going to have to be a lot more specific than "matching substrings."

Written by cdhowie

Sorry, you're totally right. I meant matching the longest possible substring.

Written by ubermensch

Accepted Answer

This would be the answer. Ready-to-use PHP function.

Written by Jon
This page was build to provide you fast access to the question and the direct accepted answer.
The content is written by members of the stackoverflow.com community.
It is licensed under cc-wiki