在字符串中搜索单词并根据另一个字符串拆分连接的字符串

Search word in string and Split concatenated string based on another string

我如何在 php 中执行以下操作?

我有两个输入 $bankdata 和 $databasedata。

问题:

如果字符串中的单词与其他字符串单词匹配,则拆分字符串中的单词

在下面的情况下,空格较多的字符串将被视为基本字符串 $databasedata 将被视为基本字符串,然后将其与每个单词的 $bankdata 进行比较,然后拆分 DHANALAKSHMI 然后重写

$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 此代码将检查另一个字符串中的单词并将其替换为额外的 space。您需要提出更多条件,因为它非常简单。希望对你有帮助。

$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

<?php

function swap(&$bankdata, &$databasedata)
{
    // get the spaces from each word
    // we use preg_replace to remove additional spaces in any of the words.
    $bank_arr = explode(' ', preg_replace('/\s+/', ' ', $bankdata));
    $data_arr = explode(' ' , preg_replace('/\s+/', ' ', $databasedata));

    $bank_space_count = count($bank_arr);
    $data_space_count = count($data_arr);

    $fixUp = function ($words, $with)
    {
        $patterns = [];
        $replacements = [];
        foreach($with as $each_word) {
            // skip single characters
            if (strlen($each_word) == 1) {
                continue;
            }

            $patherns[] = "/{$each_word}(\w\s+)?/";
            $replacements[] = "[=10=] ";
        }

        return preg_replace($patherns, $replacements, $words);
    };


    if ($bank_space_count >= $data_space_count) {
        $databasedata = $fixUp($databasedata, $bank_arr);
    }
    else {
        $bankdata = $fixUp($bankdata, $data_arr);
    }
}

$bankdata      = 'DHANALAKSHMI  K';
$databasedata  = 'DHANA LAXMI KOILADA';

swap($bankdata, $databasedata);


var_dump($bankdata, $databasedata);

3v4l 这里
Edited 3v4l