PHP 比较两个字符串中有一个字符不同,通配符方法
PHP compare two string with one character different in both, wildcard approach
需要比较两个字符串以获得 PAIR,条件是只有第 5 个字符必须不同......在 mysql 中,它可以通过 INBXOLC800Y = INBX_LC800Y(使用 '_ ' 通配符)但是如何在 PHP 中执行此操作...这是我到目前为止的代码,但我想可能会有更智能的 and/or 最短路径 ???
$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/>";
}
将第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
"with only condition that only 5th character must be different"
$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
}
试试这个功能:
function isOnlyThe5thCharacterDifferent($inp1,$inp2)
{
if ($inp1[4]!=$inp2[4])
{
$inp1[4]=$inp2[4];
return ($inp1==$inp2);
}
return false;
}
查看实际效果:https://3v4l.org/fn2sG
使用这段代码,您可以轻松配置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;
}
在这里您可以定义您的自定义通配符或将其与来自
"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 ) );
需要比较两个字符串以获得 PAIR,条件是只有第 5 个字符必须不同......在 mysql 中,它可以通过 INBXOLC800Y = INBX_LC800Y(使用 '_ ' 通配符)但是如何在 PHP 中执行此操作...这是我到目前为止的代码,但我想可能会有更智能的 and/or 最短路径 ???
$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/>";
}
将第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
"with only condition that only 5th character must be different"
$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
}
试试这个功能:
function isOnlyThe5thCharacterDifferent($inp1,$inp2)
{
if ($inp1[4]!=$inp2[4])
{
$inp1[4]=$inp2[4];
return ($inp1==$inp2);
}
return false;
}
查看实际效果:https://3v4l.org/fn2sG
使用这段代码,您可以轻松配置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;
}
在这里您可以定义您的自定义通配符或将其与来自 "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 ) );