简体   繁体   English

提取两个文本之间更改的所有单词 PHP

[英]Extract all words changed between two texts PHP

I need to compare two texts that will always be the same except 15 0 20 words, which will be replaced by others.我需要比较两个文本,除了 15 0 20 个单词外,它们总是相同的,这些单词将被其他单词替换。 How can I compare those two texts and print out the words that have been replaced?如何比较这两个文本并打印出已替换的单词?

1 Hi my friend, this is a question for stackoverflow 1 嗨,我的朋友,这是一个关于 stackoverflow 的问题
2 Hi men, this is a quoted for web 2 嗨,伙计们,这是网络引用

Results: my friend -> men结果:我的朋友 -> 男人
question -> quoted问题 -> 引用
stackoverflow -> web stackoverflow -> 网络

Thank you all谢谢你们

So, one approach would be to identify what words each string has in common.因此,一种方法是确定每个字符串有哪些共同点。 Then, for each text, capture the characters in between the common words.然后,对于每个文本,捕获常用词之间的字符。

function findDifferences($one, $two)
{
    $one .= ' {end}';  // add a common string to the end
    $two .= ' {end}';  // of each string to end searching on.

    // break sting into array of words
    $arrayOne = explode(' ', $one);
    $arrayTwo = explode(' ', $two);

    $inCommon = Array();  // collect the words common in both strings
    $differences = null;  // collect things that are different in each

    // see which words from str1 exist in str2
    $arrayTwo_temp = $arrayTwo;
    foreach ($arrayOne as $i => $word) {
        if ($key = array_search($word, $arrayTwo_temp) !== false) {
            $inCommon[] = $word;
            unset($arrayTwo_temp[$key]);
        }
    }

    $startA = 0;
    $startB = 0;

    foreach ($inCommon as $common) {
        $uniqueToOne = '';
        $uniqueToTwo = '';

        // collect chars between this 'common' and the last 'common'
        $endA = strpos($one, $common, $startA);
        $lenA = $endA - $startA;
        $uniqueToOne = substr($one, $startA, $lenA);

        //collect chars between this 'common' and the last 'common'
        $endB = strpos($two, $common, $startB);
        $lenB = $endB - $startB;
        $uniqueToTwo = substr($two, $startB, $lenB);

        // Add old and new values to array, but not if blank.
        // They should only ever be == if they are blank ''
        if ($uniqueToOne != $uniqueToTwo) {
            $differences[] = Array(
                'old' => trim($uniqueToOne),
                'new' => trim($uniqueToTwo)
            );
        }

        // set the start past the last found common word
        $startA = $endA + strlen($common);
        $startB = $endB + strlen($common);
    }

    // returns false if there aren't any differences
    return $differences ?: false;
}

Then it's a trivial matter to display the data however you want:然后根据需要显示数据是一件小事:

$one = '1 Hi my friend, this is a question for stackoverflow';
$two = '2 Hi men, this is a quoted for web';

$differences = findDifferences($one, $two);

foreach($differences as $diff){
    echo $diff['old'] . ' -> ' . $diff['new'] . '<br>';
}

// 1 -> 2
// my friend, -> men,
// question -> quoted
// stackoverflow -> web

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM