Old but good post. I took the liberty to create another code, structural type, but you can easily make it OOP.
<?php
$old = 'Text scris manual direct intr-o ceva de altceva variabila !';
$new = 'Text scris de mana pentru comparatie, si plasat direct in variabila !';
$oldArr = pregsplit('/\s+/', $old);// old (initial) text splitted into words
$newArr = pregsplit('/\s+/', $new);// new text splitted into words
$resArr = array();
$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
arraypush($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 = arraysearch($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++){
arraypush($resArr,'<span class="new-word">'.$newArr[$p].'</span>');
}
// after that, we insert the found word as unmodified
arraypush($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;
}
}
Old but good post. I took the liberty to create another code, structural type, but you can easily make it OOP.
<?php
$old = 'Text scris manual direct intr-o ceva de altceva variabila !';
$new = 'Text scris de mana pentru comparatie, si plasat direct in variabila !';
$oldArr = pregsplit('/\s+/', $old);// old (initial) text splitted into words
$newArr = pregsplit('/\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
arraypush($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 = arraysearch($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++){
arraypush($resArr,'<span class="new-word">'.$newArr[$p].'</span>');
}
// after that, we insert the found word as unmodified
arraypush($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;
}
}
echo '<br><br>';
echo $old.'<br>';
echo $new.'<br><br>';
$textFinal = '';
foreach($resArr as $val){
$textFinal .= $val.' ';
}
echo $textFinal;
?>
<style>
.new-word{background:rgba(245,255,178,1.00)}
.new-word:after{content:' '; background:rgba(245,255,178,1.00)}
.old-word{text-decoration:none; position:relative}
.old-word:after{
content: ' ';
font-size: inherit;
display: block;
position: absolute;
right: 0;
left: 0;
top: 55%;
bottom: 30%;
border-top: 1px solid #000;
border-bottom: 1px solid #000;
}
</style>
Just run the script and you'll see the results :) .