根据数组中的部分字符串检查/匹配字符串
Check / match string against partial strings in array
我正在尝试将完整的英国邮政编码与部分邮政编码进行匹配。
获取用户邮政编码,即 g22 1pf,并查看数组/数据库中是否存在匹配/部分匹配。
//Sample data
$postcode_to_check= 'g401pf';
//$postcode_to_check= 'g651qr';
//$postcode_to_check= 'g51rq';
//$postcode_to_check= 'g659rs';
//$postcode_to_check= 'g40';
$postcodes = array('g657','g658','g659','g659pf','g40','g5');
$counter=0;
foreach($postcodes as $postcode){
$postcode_data[] = array('id' =>$counter++ , 'postcode' => $postcode, 'charge' => '20.00');
}
我确实有一些代码,但那只是比较数据库中固定长度的字符串。我需要数组/数据库中的字符串长度是动态的。
数据库可能包含 "g22" 这将是一个匹配项,它也可能包含或多或少的邮政编码,即 "g221" 或 "g221p" 这也是一个匹配项。它可能包含 "g221q" 或 "g221qr" 这些不匹配。
感谢帮助,谢谢
编辑。
我可能想多了。以下伪代码似乎按预期运行。
check_delivery('g401pf');
//this would match because g40 is in the database.
check_delivery('g651dt');
// g651dt this would NOT match because g651dt is not in the database.
check_delivery('g524pq');
//g524pq this would match because g5 is in the database.
check_delivery('g659pf');
//g659pf this would match because g659 is in the database.
check_delivery('g655pf');
//g655pf this would not match, g665 is not in the database
//expected output, 3 matches
function check_delivery($postcode_to_check){
$postcodes = array('g657','g658','g659','g659pf','g40','g5');
$counter=0;
foreach($postcodes as $postcode){
$stripped_postcode = substr($postcode_to_check,0, strlen($postcode));
if($postcode==$stripped_postcode){
echo "Matched<br><br>";
break;
}
}
}
<?php
$postcode_to_check= 'g401pf';
$arr = preg_split("/\d+/",$postcode_to_check,-1, PREG_SPLIT_NO_EMPTY);
preg_match_all('/\d+/', $postcode_to_check, $postcode);
$out = implode("",array_map(function($postcode) {return implode("",$postcode);},$postcode));
$first_char = mb_substr($arr[1], 0, 1);
$MatchingPostcode=$arr[0].''.$out.''.$first_char;
echo $MatchingPostcode;
SELECT * FROM my_table WHERE column_name LIKE '%$MatchingPostcode%';
这是一个肮脏的解决方案,但它会解决你的问题。像这样的事情应该在前端或数据库中处理,但如果你必须在 php 中完成,那么这是一个解决方案。
因此此代码将匹配您包含 g401p
的任何内容。如果您不想在结尾的开头进行匹配,只需从您不想匹配的部分中删除 %
即可。在我的情况下,我为您提供它将搜索具有 g401p
的每个列记录
检查长度,把你要比较的那个剥去一样的长度。
函数check_delivery($postcode_to_check){
$postcodes = array('g657','g658','g659','g659pf','g40','g5');
$counter=0;
foreach($postcodes as $postcode){
$stripped_postcode = substr($postcode_to_check,0, strlen($postcode));
if($postcode==$stripped_postcode){
echo "Matched<br><br>";
break;
}
}
}
我正在尝试将完整的英国邮政编码与部分邮政编码进行匹配。 获取用户邮政编码,即 g22 1pf,并查看数组/数据库中是否存在匹配/部分匹配。
//Sample data
$postcode_to_check= 'g401pf';
//$postcode_to_check= 'g651qr';
//$postcode_to_check= 'g51rq';
//$postcode_to_check= 'g659rs';
//$postcode_to_check= 'g40';
$postcodes = array('g657','g658','g659','g659pf','g40','g5');
$counter=0;
foreach($postcodes as $postcode){
$postcode_data[] = array('id' =>$counter++ , 'postcode' => $postcode, 'charge' => '20.00');
}
我确实有一些代码,但那只是比较数据库中固定长度的字符串。我需要数组/数据库中的字符串长度是动态的。
数据库可能包含 "g22" 这将是一个匹配项,它也可能包含或多或少的邮政编码,即 "g221" 或 "g221p" 这也是一个匹配项。它可能包含 "g221q" 或 "g221qr" 这些不匹配。
感谢帮助,谢谢
编辑。
我可能想多了。以下伪代码似乎按预期运行。
check_delivery('g401pf');
//this would match because g40 is in the database.
check_delivery('g651dt');
// g651dt this would NOT match because g651dt is not in the database.
check_delivery('g524pq');
//g524pq this would match because g5 is in the database.
check_delivery('g659pf');
//g659pf this would match because g659 is in the database.
check_delivery('g655pf');
//g655pf this would not match, g665 is not in the database
//expected output, 3 matches
function check_delivery($postcode_to_check){
$postcodes = array('g657','g658','g659','g659pf','g40','g5');
$counter=0;
foreach($postcodes as $postcode){
$stripped_postcode = substr($postcode_to_check,0, strlen($postcode));
if($postcode==$stripped_postcode){
echo "Matched<br><br>";
break;
}
}
}
<?php
$postcode_to_check= 'g401pf';
$arr = preg_split("/\d+/",$postcode_to_check,-1, PREG_SPLIT_NO_EMPTY);
preg_match_all('/\d+/', $postcode_to_check, $postcode);
$out = implode("",array_map(function($postcode) {return implode("",$postcode);},$postcode));
$first_char = mb_substr($arr[1], 0, 1);
$MatchingPostcode=$arr[0].''.$out.''.$first_char;
echo $MatchingPostcode;
SELECT * FROM my_table WHERE column_name LIKE '%$MatchingPostcode%';
这是一个肮脏的解决方案,但它会解决你的问题。像这样的事情应该在前端或数据库中处理,但如果你必须在 php 中完成,那么这是一个解决方案。
因此此代码将匹配您包含 g401p
的任何内容。如果您不想在结尾的开头进行匹配,只需从您不想匹配的部分中删除 %
即可。在我的情况下,我为您提供它将搜索具有 g401p
检查长度,把你要比较的那个剥去一样的长度。
函数check_delivery($postcode_to_check){
$postcodes = array('g657','g658','g659','g659pf','g40','g5');
$counter=0;
foreach($postcodes as $postcode){
$stripped_postcode = substr($postcode_to_check,0, strlen($postcode));
if($postcode==$stripped_postcode){
echo "Matched<br><br>";
break;
}
}
}