其他字符串中的相似子字符串 PHP
similar substring in other string PHP
如何通过前缀或后缀检查 PHP 中的子字符串。
例如,我将搜索字符串命名为 $to_search
,如下所示:
$to_search = "abcdef"
判断是否是$to_search中子串的三种情况如下:
$cases = ["abc def", "def", "deff", ... Other values ...];
现在我必须使用 substr()
函数检测前三种情况。
如何在 PHP.
中将 "abc def", "def", "deff"
检测为 "abcdef"
的子字符串
这将查找 $cases 中的任何字符串是否是 $to_search.
的子字符串
foreach($cases as $someString){
if(strpos($to_search, $someString) !== false){
// $someString is found inside $to_search
}
}
只有 "def" 是因为其他字符串中的 none 彼此有很大关系。
另一边也没有;它是前缀和后缀而不是后缀。
您可能会发现这两个词之间的 Levenshtein distance 很有用 - 对于 abc def
,它的值为 1
。但是,您的问题定义不明确 - "similar" 匹配的字符串并不意味着任何具体内容。
编辑 - 如果您将删除成本设置为 0,那么这将非常接近您提出的问题。只需检查数组中所有内容的编辑距离是否小于 1。
您可以像这样使用 array_filter
函数:
$cases = ["cake", "cakes", "flowers", "chocolate", "chocolates"];
$to_search = "chocolatecake";
$search = strtolower($to_search);
$arr = array_filter($cases, function($val) use ($search) { return
strpos( $search,
str_replace(' ', '', preg_replace('/s$/', '', strtolower($val))) ) !== FALSE; });
print_r($arr);
输出:
Array
(
[0] => cake
[1] => cakes
[3] => chocolate
[4] => chocolates
)
正如我在上面评论的那样,它会打印除 deff
之外的所有预期值,它不是搜索字符串 abcdef
的一部分。
要查找以搜索字符串的开头或结尾开头或结尾的任何情况,我不知道还有什么方法可以做到这一点,只能遍历所有可能的开头和结尾结束组合并检查它们。可能有更好的方法来执行此操作,但应该这样做。
$to_search = "abcdef";
$cases = ["abc def", "def", "deff", "otherabc", "noabcmatch", "nodefmatch"];
$matches = array();
$len = strlen($to_search);
for ($i=1; $i <= $len; $i++) {
// get the beginning and end of the search string of length $i
$pre_post = array();
$pre_post[] = substr($to_search, 0, $i);
$pre_post[] = substr($to_search, -$i);
foreach ($cases as $case) {
// get the beginning and end of each case of length $i
$pre = substr($case, 0, $i);
$post = substr($case, -$i);
// check if any of them match
if (in_array($pre, $pre_post) || in_array($post, $pre_post)) {
// using the case as the array key for $matches will keep it distinct
$matches[$case] = true;
}
}
}
// use array_keys() to get the keys back to values
var_dump(array_keys($matches));
如何通过前缀或后缀检查 PHP 中的子字符串。
例如,我将搜索字符串命名为 $to_search
,如下所示:
$to_search = "abcdef"
判断是否是$to_search中子串的三种情况如下:
$cases = ["abc def", "def", "deff", ... Other values ...];
现在我必须使用 substr()
函数检测前三种情况。
如何在 PHP.
"abc def", "def", "deff"
检测为 "abcdef"
的子字符串
这将查找 $cases 中的任何字符串是否是 $to_search.
的子字符串foreach($cases as $someString){
if(strpos($to_search, $someString) !== false){
// $someString is found inside $to_search
}
}
只有 "def" 是因为其他字符串中的 none 彼此有很大关系。
另一边也没有;它是前缀和后缀而不是后缀。
您可能会发现这两个词之间的 Levenshtein distance 很有用 - 对于 abc def
,它的值为 1
。但是,您的问题定义不明确 - "similar" 匹配的字符串并不意味着任何具体内容。
编辑 - 如果您将删除成本设置为 0,那么这将非常接近您提出的问题。只需检查数组中所有内容的编辑距离是否小于 1。
您可以像这样使用 array_filter
函数:
$cases = ["cake", "cakes", "flowers", "chocolate", "chocolates"];
$to_search = "chocolatecake";
$search = strtolower($to_search);
$arr = array_filter($cases, function($val) use ($search) { return
strpos( $search,
str_replace(' ', '', preg_replace('/s$/', '', strtolower($val))) ) !== FALSE; });
print_r($arr);
输出:
Array
(
[0] => cake
[1] => cakes
[3] => chocolate
[4] => chocolates
)
正如我在上面评论的那样,它会打印除 deff
之外的所有预期值,它不是搜索字符串 abcdef
的一部分。
要查找以搜索字符串的开头或结尾开头或结尾的任何情况,我不知道还有什么方法可以做到这一点,只能遍历所有可能的开头和结尾结束组合并检查它们。可能有更好的方法来执行此操作,但应该这样做。
$to_search = "abcdef";
$cases = ["abc def", "def", "deff", "otherabc", "noabcmatch", "nodefmatch"];
$matches = array();
$len = strlen($to_search);
for ($i=1; $i <= $len; $i++) {
// get the beginning and end of the search string of length $i
$pre_post = array();
$pre_post[] = substr($to_search, 0, $i);
$pre_post[] = substr($to_search, -$i);
foreach ($cases as $case) {
// get the beginning and end of each case of length $i
$pre = substr($case, 0, $i);
$post = substr($case, -$i);
// check if any of them match
if (in_array($pre, $pre_post) || in_array($post, $pre_post)) {
// using the case as the array key for $matches will keep it distinct
$matches[$case] = true;
}
}
}
// use array_keys() to get the keys back to values
var_dump(array_keys($matches));