需要比较两个字符串以获得一个只有第五个字符必须不同的条件的PAIR ...在mysql中它可以通过INBXOLC800Y = INBX_LC800Y(使用'_'通配符)实现但是如何在PHP中执行此操作...这里是我的代码现在停止,但我想可能有更聪明和/或最短的方式???
$first_sku_full = "INBXOLC800Y"; // with O
$second_sku_full = "INBXTLC800Y"; // with T
$first_sku_first_part= substr($first_sku_full,0,4); //gives first 4 characters INBX
$second_sku_first_part= substr($second_sku_full,0,4); //gives first 4 characters INBX
if($first_sku_first_part == $second_sku_first_part){ // matching first 4 characters
$first_sku_last_part= substr($first_sku_full, 5); // gives 6th onward characters i.e, LC800Y
$second_sku_last_part= substr($second_sku_full, 5); // gives 6th onward characters i.e, LC800Y
if ( $first_sku_last_part == $second_sku_last_part ) { // matching 6th and onward characters
if ( $first_sku_full != $second_sku_full ) { // matching full strings
echo "first and second sku is a pair <br/>";
}else{
echo "NOT a pair , both SKUs are same <br/>";
}
}else{
echo "NOT a pair , last part of string not matched $first_sku_last_part vs $second_sku_last_part <br/>";
}
}else{
echo "NOT a pair , first part of string not matched $first_sku_first_part vs $second_sku_first_part<br/>";
}
答案 0 :(得分:2)
“只有条件,只有第5个字符必须不同”
$first_sku_full = "INBXOLC800Y"; // with O
$second_sku_full = "INBXTLC800Y";
if($first_sku_full[4] != $second_sku_full[4]) {
//fifth character is different in case above it will return true because O != T
}
这样你可以检查第五个字符是否不同。您可以在PHP中使用数组运算符访问字符串字母。
如果你想比较没有5个字符的其余字符串,你可以按照下面这样做
$first_sku_full = "INBXOLC800Y"; // with O
$second_sku_full = "INBXTLC800Y";
$first_sku_full[4] = $second_sku_full[4] = '';
if($first_sku_full == $second_sku_full) {
//the strings are same despite 5th character
}
答案 1 :(得分:2)
尝试此功能:
function isOnlyThe5thCharacterDifferent($inp1,$inp2)
{
if ($inp1[4]!=$inp2[4])
{
$inp1[4]=$inp2[4];
return ($inp1==$inp2);
}
return false;
}
在行动中看到它:https://3v4l.org/fn2sG
答案 2 :(得分:1)
用常量字符串替换第5个字符
$first_sku_full_masked = substr_replace($first_sku_full, '#', 4, 1);
$second_sku_full_masked = substr_replace($second_sku_full, '#', 4, 1);
然后比较。
这给了
INBX#LC800Y
INBX#LC800Y
将被视为平等。所以条件是
$first_sku_full_masked==$second_sku_full_masked && $first_sku_full!=$second_sku_full
答案 3 :(得分:0)
使用此代码,您可以轻松配置必须不相等的 N 位置
<?php
$first_sku_full = "INBXPLC800YF"; // with O
$second_sku_full = "INBXTLC800YD"; // with T
var_dump(isEqual($first_sku_full, $second_sku_full, array(4, 11)));
/**
* @param1 string $str1
* @param1 string $str2
* @param1 array $pos
* @return boolean
*/
function isEqual($str1, $str2, $pos) {
if (strlen($str1) != strlen($str2)) return FALSE;
for ($i=0 ; $i< strlen($str1) ; $i++) {
if ( in_array($i, $pos) && $str1[$i] == $str2[$i]) return FALSE;
if (!in_array($i, $pos) && $str1[$i] != $str2[$i]) return FALSE;
}
return TRUE;
}
答案 4 :(得分:0)
您可以在此处定义自定义通配符或将其与解决方案结合使用 “Halayem Anis”定义了多个通配符
// just an example array
$arrayToCheck = array(
"INBXOLC800Y",
"INBXOLC809Y",
"INBXTLC800Y",
"INBXALC800Y",
);
function compareWithWildcard( $stringToCompare, $compareWith, $wildcard = "_" ) {
if( !$wildcard ) return ( $stringToCompare == $compareWith );
$wildcardIndex = strpos( $compareWith, $wildcard );
if( $wildcardIndex !== false ) {
$stringToCompare[$wildcardIndex] = $wildcard;
}
return ( $stringToCompare == $compareWith );
}
$compareTo = "INBX_LC800Y";
var_dump( compareWithWildcard( $arrayToCheck[0], $compareTo ) );