[英]Check if substring of a string is substring of another string
I have two strings 我有两个字符串
$Str1 = "A B C D E F G H I J";
$Str2 = "1 2 C D E P Q R";
Now I have to check that a substring of $Str2
exits in $Str1
. 现在我必须检查
$Str2
的子字符串是否退出$Str1
。 For example in this case CDE . 例如在这种情况下CDE 。
This is a basic and simple example. 这是一个基本而简单的例子。 I am having complex cases and data in both strings is dynamic.
我遇到了复杂的情况,两个字符串中的数据都是动态的。
If I would had to compare complete string the strpos was the solution but what to do in this case? 如果我不得不比较完整的字符串strpos是解决方案,但在这种情况下该怎么办?
This function should work any Strings 这个函数适用于任何字符串
function test($str1, $str2)
{
for ($i = strlen($str1); $i != 0; $i--) {
for ($x = 0; $x < strlen($str1) - $i; $x++) {
if (($pos = strpos($str2, substr($str1, $x, $i + 1))) !== false) {
return substr($str1, $x, $i + 1);
}
}
}
return null;
}
It returns the biggest possible match or null. 它返回最大可能的匹配或null。
$str1 = "Sed ut perspiciatis unde omnis iste natus error sit voluptatem";
$str2 = "Sed ut perspiciatis undeT omnis iste natus error sit voluptatem";
echo test($str1, $str2); // omnis iste natus error sit voluptatem
This function should work for you: 这个功能应该适合你:
<?php
$Str1 = "A B C D E F G H I J";
$Str2 = "1 2 C D E P Q R";
echo strCompare($Str1, $Str2); //Returns C D E
function strCompare($Str1, $Str2) {
$str1 = explode(" ", $Str1);
$str2 = explode(" ", $Str2);
$match = false;
$newStr = [];
foreach($str1 as $key => $value){
if($match == true){
if(isset($str2[$key]) && $str2[$key] == $value){
$newStr[] = $value;
} else {
break;
}
} else {
if(isset($str2[$key]) && $str2[$key] == $value){
$match = true;
$newStr[] = $value;
}
}
};
return implode(" ",$newStr);
}
maybe this one might help too: 也许这个可能也有帮助:
<?php
/**
* returns an array of positions and substrings of needle that were found
* in string haystack using a minimal and maximal substring length
*
* @param unknown $haystack find substrings of needle in this string
* @param unknown $needle substrings in needle will be searched in haystack
* @param number $min minimal length of a substring
* @param number $max maximum length of a substring
* @return array empty if no result
*/
function intersect_string($haystack,$needle,$min=3,$max=25)
{
$r = [];
for($j=$i=0;isset($needle[$i])&&isset($needle[$i+1]);$j=0,$i++) {
if (!($c=$needle[$i].$needle[$i+1])) continue;
while (($p=strpos($haystack,$c,$j))!==false) {
$r[$p] = $c;
for ($j=$p+2,++$i;isset($needle[$i+1])&&isset($haystack[$j]);$j++,$i++) {
if (!($needle[$i+1]==$haystack[$j])) break;
$r[$p] .= $haystack[$j];
}
$l = strlen($r[$p]);
if ($l<$min||$l>$max) { unset($r[$p]); }
}
}
return $r;
}
header('Content-Type:text/plain');
$str1 = "A little wolf is jumping over the tiny cow";
$str2 = "A tiny cow is jumping over the little wolf";
var_dump(intersect_string($str1,$str2,2));
var_dump(intersect_string($str1,$str2,3,12));
$str1 = "A B C D E F G H I J";
$str2 = "1 2 C D E P Q R";
var_dump(intersect_string($str1,$str2,1));
var_dump(intersect_string($str1,$str2,8));
$str1 = "Sed ut perspiciatis unde omnis iste natus error sit voluptatem";
$str2 = "Sed ut perspiciatis undeT omnis iste natus error sit voluptatem";
var_dump(intersect_string($str1,$str2,3,40));
?>
this will result in: 这将导致:
array(4) {
[0]=>
string(2) "A "
[34]=>
string(8) "tiny cow"
[13]=>
string(21) " is jumping over the "
[2]=>
string(11) "little wolf"
}
array(2) {
[34]=>
string(8) "tiny cow"
[2]=>
string(11) "little wolf"
}
array(1) {
[3]=>
string(7) " C D E "
}
bool(false)
array(2) {
[0]=>
string(24) "Sed ut perspiciatis unde"
[24]=>
string(38) " omnis iste natus error sit voluptatem"
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.