我该如何在php中执行以下操作?
我有两个输入$bankdata和$databasedata.
问题:
如果与其他字符串单词匹配,则在字符串中拆分单词
在以下情况下,具有更多空格的字符串将被视为基本字符串:$databasedata将被视为基本字符串,然后将其与每个单词的$bankdata比较,然后拆分DHANA和LAKSHMI,然后重写
$bankdata = DHANA LAKSHMI K
例如 :
Input :
$bankdata = 'DHANALAKSHMI K'
$databasedata = 'DHANA LAXMI KOILADA'
Output :
$bankdata = 'DHANA LAKSHMI K' //This willchanged
$databasedata = 'DHANA LAXMI KOILADA'
Input :
$bankdata = 'P VENKATESWARA RAO'
$databasedata = 'PARELLA VENKATESWARARAO'
Output :
$bankdata = 'P VENKATESWARA RAO'
$databasedata = 'PARELLA VENKATESWARA RAO' //This willchanged
Input :
$bankdata = 'LINGAREDDY S'
$databasedata = 'LINGA REDDY SHEELAM'
Output :
$bankdata = 'LINGA REDDY S' //This will changed
$databasedata = 'LINGA REDDY SHEELAM'
Input :
$bankdata = 'ROSHAN LAL'
$databasedata = 'ROSHANLAL X'
Output :
$bankdata = 'ROSHAN LAL'
$databasedata = 'ROSHAN LAL X' //This will changed
解决方法:
您好,@ Daoootim,此代码将检查另一个字符串中的单词,并将其替换为额外的空格.您需要做出更多条件,因为这非常简单.希望对您有所帮助.
$bankdata = 'P VENKATESWARARAO RAO' ;
$databasedata = 'PARELLA VENKATESWARARAORAO';
echo 'INPUT: ' . $databasedata . '<br>';
echo 'INPUT: ' . $bankdata . '<br>';
$inputArray1 = explode(' ',$bankdata);
$inputArray2 = explode(' ',$databasedata);
foreach ($inputArray1 as $word) {
if (strlen($word) > 1){
foreach ($inputArray1 as $reference) {
if ($reference != $word){
if (strpos($reference,$word) > 0) continue 2;
}
}
$databasedata = str_replace($word, $word." ",$databasedata);
}
}
foreach ($inputArray2 as $word) {
if (strlen($word) > 1){
foreach ($inputArray2 as $reference) {
if ($reference != $word){
if (strpos($reference,$word) > 0) continue 2;
}
}
$bankdata = str_replace($word, $word." ",$bankdata);
}
}
echo 'OUTPUT: ' . $databasedata . '<br>';
echo 'OUTPUT: ' . $bankdata . '<br>';
输入:PARELLA VENKATESWARARAORAO
输入:P VENKATESWARARAO RAO
输出:PARELLA VENKATESWARARAO RAO
输出:P VENKATESWARARAO RAO