text
  • compare
  • highlight
  • difference
  • words
  • 2017-01-21 2 views 1 likes 
    1

    두 텍스트를 비교하여 다른 단어로 강조 표시하는 scrpit을 가지고 있지만 전혀 작동하지 않습니다. 많은 단어들은 그것들이 그렇지 않을 때 그들을 다른 것으로 표시합니다. 예를 들어, "그" "그"등은 그것들을 고려하지 않으며, 두 단어 사이에 있다면, 또한 변경된 것으로 변경된 것으로 표시됩니다. 나는 이미지를 붙인다.두 텍스트를 비교하고 차이점 만 강조 표시합니다.

    <?php 
    
    
    $old = 'The one-page order, which Mr. Trump signed in a hastily arranged Oval Office ceremony shortly before departing for the inaugural balls, gave no specifics about which aspects of the law it was targeting. But its broad language gave federal agencies wide latitude to change, delay or waive provisions of the law that they deemed overly costly for insurers, drug makers, doctors, patients or states, suggesting that it could have wide-ranging impact, and essentially allowing the dismantling of the law to begin even before Congress moves to repeal it.'; 
    
    
    $new = 'The one-page order, which Mr. Trump signed in a unexpectedly organized Oval workplace rite quickly before departing for the inaugural balls, gave no specifics approximately which components of the law it became targeting. But its large language gave federal organizations huge range to exchange, put off or waive provisions of the law that they deemed overly luxurious for insurers, drug makers, docs, sufferers or states, suggesting that it could have wide-ranging effect, and basically permitting the dismantling of the regulation to start even before Congress moves to repeal it.'; 
    
    
    
    $oldArr = preg_split('/\s+/', $old);// old (initial) text splitted into words 
    $newArr = preg_split('/\s+/', $new);// new text splitted into words 
    $resArr = array(); 
    
    $oldCount = count($oldArr)-1; 
    $newCount = count($newArr)-1; 
    
    $tmpOld = 0;// marker position for old (initial) string 
    $tmpNew = 0;// marker position for new (modified) string 
    $end = 0;// do while variable 
    
    // endless do while loop untill specified otherwise 
    while($end == 0){ 
    // if marker position is less or equal than max count for initial text 
    // to make sure we don't overshoot the max lenght 
    if($tmpOld <= $oldCount){ 
    // we check if current words from both string match, at the current marker positions 
    if($oldArr[$tmpOld] === $newArr[$tmpNew]){ 
    // if they match, nothing has been modified, we push the word into results and increment both markers 
    array_push($resArr,$oldArr[$tmpOld]); 
    $tmpOld++; 
    $tmpNew++; 
    }else{ 
    // fi the words don't match, we need to check for recurrence of the searched word in the entire new string 
    $foundKey = array_search($oldArr[$tmpOld],$newArr,TRUE); 
    // if we find it 
    if($foundKey != '' && $foundKey > $tmpNew){ 
    // we get all the words from the new string between the current marker and the foundKey exclusive 
    // and we place them into results, marking them as new words 
    for($p=$tmpNew;$p<$foundKey;$p++){ 
    array_push($resArr,'<span class="new-word">'.$newArr[$p].'</span>'); 
    } 
    // after that, we insert the found word as unmodified 
    array_push($resArr,$oldArr[$tmpOld]); 
    // and we increment old marker position by 1 
    $tmpOld++; 
    // and set the new marker position at the found key position, plus one 
    $tmpNew = $foundKey+1; 
    }else{ 
    // if the word wasn't found it means it has been deleted 
    // and we need to add ti to results, marked as deleted 
    array_push($resArr,'<span class="old-word">'.$oldArr[$tmpOld].'</span>'); 
    // and increment the old marker by one 
    $tmpOld++; 
    } 
    } 
    }else{ 
    $end = 1; 
    } 
    } 
    
    $textFinal = ''; 
    foreach($resArr as $val){ 
    $textFinal .= $val.' '; 
    } 
    echo "<p>".$textFinal."</p>"; 
    ?> 
    <style> 
    body { 
    background-color: #2A2A2A; 
    } 
    
    @font-face { 
    font-family: 'Eras Light ITC'; 
    font-style: normal; 
    font-weight: normal; 
    src: local('Eras Light ITC'), url('ERASLGHT.woff') format('woff'); 
    } 
    
    p { 
    font-family: 'Eras Light ITC', Arial; 
    color:white; 
    } 
    
    .new-word{background:rgba(1, 255, 133, 0.9);color:black;font-weight: bold;} 
    .new-word:after{background:rgba(1, 255, 133, 0.9)} 
    .old-word{text-decoration:none; position:relative;background:rgba(215, 40, 40, 0.9);} 
    .old-word:after{ 
    
    
    } 
    </style> 
    

    예 : 그들은 변경하지 않은 경우

    Example image result

    왜 당신이 그 다른 단어를 표시합니까? 감사합니다.

    답변

    0

    코드를 검사하여 다른 경우를 시도했지만 알고리즘이 잘못되었다고 생각합니다.

    예를 들어 "for"또는 "the"대신 "one-page"를 입력하면 "unmatch"처럼 보입니다. 이것의 뒤에 이유는, 불일치가있을 때, 당신은 모든 배열에서 일치하지 않는 단어를 찾고 있습니다. 그런 다음 주어진 단어가 이미 건너 뛴 경우 (색인 번호가 적은 경우) 알고리즘이 실패합니다.

    다음 변수를 사용할 수 있습니다. 비용이 많이 드는-고급스러운 불일치 발견이 설정에 대해서는

    $old = 'for costly for insurers.'; 
    $new = 'for luxurious for insurers.'; 
    

    , 당신의 코드는 단어 "를"다음과 일치하도록 시도합니다. 그러나 array_search 호출은 문자열의 시작 부분에 "for"의 위치를 ​​반환합니다.

    $foundKey = array_search($oldArr[$tmpOld],$newArr,TRUE); 
    

    다른 방법으로 검색하려면이 섹션을 수정해야합니다. "starting_indices"기능을 가진 array_search를 코딩 할 수 있습니다. (또는 배열에서 성공적으로 일치하는 요소의 설정을 해제 할 수 있습니다.)

    +0

    오류를 처리 할 수 ​​없습니다. 내가 막혔는데 어떻게 해결할 수 있을지 모르겠다. – JotaMarkes

    관련 문제