Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/253.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 提取两个文本之间更改的所有单词_Php_Text_Compare_Extract_Words - Fatal编程技术网

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

Php 提取两个文本之间更改的所有单词,php,text,compare,extract,words,Php,Text,Compare,Extract,Words,我需要比较两个文本,这两个文本将始终是相同的,除了15 0 20个单词,这将被其他替换。我如何比较这两个文本并打印出被替换的单词 1嗨,朋友,这是stackoverflow的问题 2嗨,伙计们,这是一个网站的报价 结果: 我的朋友->男人 问题->引用 stackoverflow->web 谢谢大家因此,一种方法是确定每个字符串的共同点。然后,对于每个文本,捕获常用词之间的字符 function findDifferences($one, $two) { $one .= ' {end}'

我需要比较两个文本,这两个文本将始终是相同的,除了15 0 20个单词,这将被其他替换。我如何比较这两个文本并打印出被替换的单词

1嗨,朋友,这是stackoverflow的问题
2嗨,伙计们,这是一个网站的报价

结果: 我的朋友->男人
问题->引用
stackoverflow->web


谢谢大家

因此,一种方法是确定每个字符串的共同点。然后,对于每个文本,捕获常用词之间的字符

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;
}
然后,以您想要的方式显示数据是一件小事:

$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
$one='1嗨,朋友,这是stackoverflow的问题';
$two='2嗨,这是一个网站的报价';
$differences=findDifferences($1,$2);
foreach(差异为$diff){
回显$diff['old'].->'.$diff['new'].
'; } // 1 -> 2 //我的朋友们, //问题->引用 //stackoverflow->web
添加到目前为止的代码,以查看您尝试过的内容。