在 PHP 中查找两个字符串的匹配部分
我正在寻找一种简单的方法来查找 PHP 中两个字符串的匹配部分(特别是在 URI 的上下文中)
例如,考虑两个字符串:
http://2.2.2.2/~machinehost/deployment_folder/
和
/~machinehost/deployment_folder/users/bob/settings
我需要的是砍掉这些的匹配部分第二个字符串中的两个字符串,结果是:
users/bob/settings,
然后附加第一个字符串作为前缀,形成绝对 URI。
有没有一些简单的方法(在 PHP 中)来比较两个任意字符串以匹配其中的子字符串?
编辑:正如所指出的,我的意思是两个字符串共有的最长匹配子字符串
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
假设您的字符串分别为
$a
和$b
,您可以使用以下内容:此结果为
http://2.2.2.2/~machinehost/deployment_folder/用户/鲍勃/设置
。Assuming your strings are
$a
and$b
, respectively, you can use this:This result is
http://2.2.2.2/~machinehost/deployment_folder/users/bob/settings
.查找最长的公共匹配也可以使用正则表达式来完成。
下面的函数将采用两个字符串,使用一个字符串创建正则表达式,然后针对另一个字符串执行它。
它将使用两个字符串中较短的一个来生成正则表达式,尽管两种方式的性能很可能是相同的。它可能会错误地将字符串与重复出现的子字符串进行匹配,并且我们仅限于匹配两个或多个字符的字符串,除非它们相等或者一个是另一个字符的子字符串。例如:
无论如何,它使用替代方法运行,并且可以改进正则表达式以解决其他情况。
Finding the longest common match can also be done using regex.
The below function will take two strings, use one to create a regex, and execute it against the other.
It'll generate a regex using the shorter of the two strings, although performance will most likely be the same either way. It may incorrectly match strings with recurring substrings, and we're limited to matching strings of two characters or more, unless they are equal or one is a substring of the other. For Instance:
Regardless, it functions using an alternate method and the regex can be refined to tackle additional situations.
我不确定是否理解您的完整请求,但想法是:
让 A 成为您的 URL,B 成为您的“/~machinehost/deployment_folder/users/bob/settings”,
我还没有测试过,但如果你真的需要,我可以帮助你让这个出色的(讽刺的)解决方案发挥作用。
请注意,使用正则表达式可能是可能的,例如
编辑:我认为您的最后一条评论使我的回复无效。但你想要的是找到子字符串。因此,您可以首先从一个重型算法开始,尝试在 {2, length(B)} 中找到 A 中的 B[1:i],然后使用一些 动态编程内容。
I'm not sure to understand your full request, but the idea is:
Let A be your URL and B your "/~machinehost/deployment_folder/users/bob/settings"
I have not tested yet, but if you really need, I can help you make this brilliant (ironical) solution work.
Note that it may be possible with regular expressions like
Edit: I think your last comment invalidates my response. But what you want is finding substrings. So you can first start with a heavy algorithm trying to find B[1:i] in A for i in {2, length(B)} and then use some dynamic programming stuffs.
它似乎不是满足您要求的现成代码。那么让我们寻找一种简单的方法。
在这个练习中,我使用了两种方法,一种用于查找最长的匹配,另一种用于截断匹配部分。
FindLongestMatch() 方法分解一条路径,逐段在另一条路径中寻找匹配项,只保留一个匹配项,即最长的匹配项(没有数组,没有排序)。
RemoveLongestMatch() 方法采用找到的最长匹配位置之后的后缀或“余数”。
这里是完整的源代码:
这是测试用例的代表性子集:
运行以前的测试用例会提供以下输出:
也许您可以采用这段代码的想法并将其转化为对当前项目有用的东西。
让我知道它是否也对你有用。顺便说一下,oreX先生的回答看起来也不错。
it does not seem to be an out of the box code out there for your requirement. So lets look for a simple way.
For this exercise I utilized two methods, one for finding the longest match, and another one to chop off the matching portion.
The FindLongestMatch() method, takes apart a path, piece by piece seeks for a match in the other path, keeping just one match, the longest one (no arrays, no sorting).
The RemoveLongestMatch() method takes the suffix or 'remainder' after the longest match found position.
Here the full source code:
This is a representative subset of Test Cases:
Running previous Test Cases provides the following output:
Maybe you can take the idea of this piece of code and turn it into something that you find useful for your current project.
Let me know if it worked for you too. By the way, Mr. oreX answer looks good too.
试试这个。
http://pastebin.com/GqS3UiPD
Try this.
http://pastebin.com/GqS3UiPD