[PHP] Diff html codes

Pagina: 1
Acties:

Onderwerpen


Acties:
  • 0 Henk 'm!

  • radem205
  • Registratie: Juni 2002
  • Laatst online: 02-02-2022
Ha,

Ik heb onlangs Diff (http://pear.php.net/package/Text_Diff/) geinstalleerd op de webserver. Nu maak ik gebruik van TinyMCE om klanten in de gelegenheid te stellen simpel teksten te wijzigen.

Nu heb ik een documentsysteem waarbij bezoekers documenten kunnen wijzigen, waardoor een nieuwe versie wordt aangemaakt. Nu wil ik de bezoeker de wijzigingen laten zien t.o.v. de laatste wijziging door de tekst een andere kleur te geven. Nu kan het zo zijn dat in de tekst html codes zitten.

Hoe kan ik er voor zorgen dat de html codes niet mee gaan in de check naar wijzigingen?

Alvast bedankt voor je reactie.

Acties:
  • 0 Henk 'm!

Verwijderd

Eerst strip_tags eroverheen halen. Maar dat jij een wijziging in markup geen wijziging vindt, vind ik maar raar.

Acties:
  • 0 Henk 'm!

  • radem205
  • Registratie: Juni 2002
  • Laatst online: 02-02-2022
Ja dat vind ik wel een wijziging, maar de bezoeker moet de wijziging alleen in de vorm van tekst zien en niet in de vorm van htmlcodes. Dus de html code moet naar het checken wel weer terug gezet worden.

Acties:
  • 0 Henk 'm!

  • Jurgle
  • Registratie: Februari 2003
  • Laatst online: 24-06 00:27

Jurgle

100% Compatible

Zo'n ding heb ik ook ooit eens gemaakt. Heb er iets over geschreven, http://www.edesign.nl/archives/20 wellicht dat je daar iets mee kan als je t aanpast om < en > te negeren?

My opinions may have changed but not the fact that I am right ― Ashleigh Brilliant


Acties:
  • 0 Henk 'm!

  • radem205
  • Registratie: Juni 2002
  • Laatst online: 02-02-2022
Deze maakt geen gebruik van diff toch? Maakt verder ook niet uit.

Kan je misschien mij een voorzetje geven hoe ik html codes kan laten negeren?

Acties:
  • 0 Henk 'm!

  • radem205
  • Registratie: Juni 2002
  • Laatst online: 02-02-2022
Als het mag ff een kickje ;)

Ik heb geprobeerd wijs te worden uit je code i.c.m. met je blog post, maar ik kom er echt niet uit.

Ik heb nu het volgende:

PHP:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
<?PHP
if(isset($_GET['showsource']))
    die(highlight_file(__FILE__, true));

//No automatic slash-or-quotes-behaviour I don't know about
if (get_magic_quotes_gpc()) {
   function stripslashes_deep($value)
   {
       $value = is_array($value) ?
                   array_map('stripslashes_deep', $value) :
                   stripslashes($value);

       return $value;
   }

   $_REQUEST = array_map('stripslashes_deep', $_REQUEST);
   $_POST = array_map('stripslashes_deep', $_POST);
   $_GET = array_map('stripslashes_deep', $_GET);
   $_COOKIE = array_map('stripslashes_deep', $_COOKIE);
}

function levenshteinPlus($s, $t, $wantArray = 0, $debug = 0)
{
   if(is_array($s))
   {
      $n = count($s);
      $m = count($t);
   }
   else
   {
      $n = strlen($s);
      $m = strlen($t);
   }
   
   $matrix = array();
   
   // Initialize first row with 0..n 
   for($i = 0; $i <= $n; $i++) 
      $d[$i][0] = $i; 

   // Intialize first column with 0..m 
   for($j = 0; $j <= $m; $j++) 
      $d[0][$j] = $j; 

   // Cost calculation loop 
   $maxSubCost = 0;
   for($i = 1; $i <= $n; $i++) 
   { 
      $sc = $s[($i - 1)]; 
      for($j = 1; $j <= $m; $j++) 
      { 
         if(preg_replace('/[[:punct:]]/', '', $sc) == preg_replace('/[[:punct:]]/', '', $t[($j - 1)])) // Characters are equal, no cost 
           $subCost = 0; 
         else            // Characters are not equal, costs 1 
           $subCost = 1; 
         
         $left = $d[($i - 1)][$j] + 1;               // left + deletion cost 
         $above = $d[$i][($j - 1)] + 1;              // above + insertion cose 
         $diagonal = $d[($i - 1)][($j - 1)] + $subCost;   // diagonal + substitution cost 
         $d[$i][$j] = min($above, min($left, $diagonal)); // Put minimum in current cell 
         
         if($d[$i][$j] > $maxSubCost)
            $maxSubCost = $d[$i][$j];
      } 
   } 

   $i = $n; 
   $j = $m; 

   // Find the optimal path backwards:
   if($wantArray)
   {
      $res = array();
      $stappen = 0;
      
      while(!($i == 0 && $j == 0)) 
      { 
         $stappen++;
         $left = ($i == 0)? $maxSubCost : $d[($i - 1)][$j]; 
         $above = ($j == 0)? $maxSubCost : $d[$i][($j - 1)]; 
         $diagonal = ($i == 0 || $j == 0)? $maxSubCost : $d[($i - 1)][($j - 1)]; 
         $minimal =  min($above, min($left, $diagonal)); 

         // Choose minimal of left, above and diagonal: 
         if($diagonal == $minimal) 
         { 
            $sc = $s[($i - 1)];
            $tc = $t[($j - 1)]; 

            if(preg_replace('/[[:punct:]]/', '', $sc) == preg_replace('/[[:punct:]]/', '', $tc)) 
            {
               if($debug) echo "Leave '".$tc."' the same<br>"; 
               $res[] = array('0', $tc);
            }
            else 
            {
               if($debug) echo "Substitute '".$sc."' with '".$tc."'<br>"; 
               $res[] = array('1', $sc, $tc);
            }
            
            $i--; 
            $j--; 
         } 
         else if($above == $minimal) 
         { 
            if($debug) echo "Insert '".$t[($j - 1)]."'<br>"; 
            $res[] = array('+', $t[($j - 1)]);
            $j--; 
         } 
         else // left==minimal 
         { 
            if($debug) echo "Delete '".$s[($i - 1)]."'<br>"; 
            $res[] = array('-', $s[($i - 1)]);
            $i--; 
         } 
      }
      
      $res[] = array($d[$n][$m], $stappen);
      
      $res = array_reverse($res);
      
      return $res;
   }
   return $d[$n][$m];
}


$str1 = 'The price of crude oil fell nearly $3 and settled below $62 a barrel Monday on a supply glut at a key North American delivery point as geopolitical tensions loomed.

An oversupply at the storage hub in Cushing, Okla., while localized, had a disproportionate effect on prices, said Citigroup Global Markets energy analyst Tim Evans. eDesign.nl is a cool website.

Because the hub serves as a delivery point for wood, the facility is the "Achilles heel" for the petroleum market, he said. As it nears capacity, dealers have no place to store more oil should they want to take advantage of low prices on the May futures contract, which expires April 20.';


$str2 = 'The price of crude oil fell nearly $3 and settled below $62 a barrel Monday on a supply glut at a key North American delivery point as geopolitical tensions loomed.

An oversupply at the storage hub in Cushing, Okla., while localized, had a disproportionate effect on prices, said Citigroup Global Markets energy analyst Tim Evans.

Because the hub serves as a delivery point for oil, the facility is the "Achilles heel" for the petroleum market, he said. As it nears capacity, dealers have no place to store more oil should they want to take advantage of low prices on the May futures contract, which expires April 20, Evans said.';



if(isset($_POST['source']))
    $str1 = $_POST['source'];
if(isset($_POST['target']))
    $str2 = $_POST['target'];

$res = levenshteinPlus(explode(" ", $str1), explode(" ", $str2), 1);

?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<title>Levenshtein extended</title>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
</head>
<body style="font-family: Lucida Console; font-size: 10pt;">
<h1>Levenshtein extended</h1>
<form action="<?PHP echo $_SERVER['PHP_SELF']; ?>" method="post">
<table border="0">
<tr>
 <td>Source text</td>
 <td>Target text</td>
</tr>
<tr>
 <td><textarea name="source" style="border: 1px solid black; width:500px; height:250px;" cols="62" rows="15"><?php echo htmlentities($str1); ?></textarea></td>
 <td><textarea name="target" style="border: 1px solid black; width:500px; height:250px;" cols="62" rows="15"><?php echo htmlentities($str2); ?></textarea></td>
</tr>
<tr>
 <td><a href="http://www.edesign.nl/archives/20">Levenshtein showcase</a> by <a href="http://www.edesign.nl">eDesign.nl</a> (<a href="<?php echo $_SERVER['PHP_SELF']; ?>?showsource=true">source code</a>)</td>
 <td style="text-align: right"><input type="submit" value="Compare texts" /></td>
</tr>
</table>
</form>
<h2>Results:</h2>
<?PHP 
    echo '<p>Source text has '.count(explode(' ', $str1)).' words and the target text has '.count(explode(' ', $str2)).'. The transformation needed '.round(($res[0][0]/$res[0][1])*100).'% of the source text to be modified.<br />';
?>
<br />
Compared text:</p>
<div style="border: 1px solid black; width:1009px;"><p>
<?php
$out = '';
for($i = 1; $i < count($res); $i++)
{
   if($res[$i][0] == '0')
      $out .= $res[$i][1]." ";
   else if($res[$i][0] == '1')
      $out .= "<span style='color: blue;'>".$res[$i][2]." </span>";
   else if($res[$i][0] == '+')
      $out .= "<span style='color: blue;'>".$res[$i][1]." </span>";
   else if($res[$i][0] == '-')
      $out .= "<span style='color: blue;'>".$res[$i][1]." </span>";
}
echo str_replace("\n", '<br />', $out);
?></p></div>

<p>
    <a href="http://validator.w3.org/check?uri=referer"><img
        src="http://www.w3.org/Icons/valid-xhtml10"
        alt="Valid XHTML 1.0 Strict" height="31" width="88" style="border: 0px;" /></a>
  </p>
  
  </body>
</html> 


Ik weet wel hoe je de tekst kan strippen van html codes, maar hoe zorg ik ervoor dat die html codes wel weer terug komen in de tekst?

Acties:
  • 0 Henk 'm!

  • Janoz
  • Registratie: Oktober 2000
  • Laatst online: 02:21

Janoz

Moderator Devschuur®

!litemod

Tja, dat is niet zo heel erg simpel. Daarvoor zul je de levenshtein methode aan moeten passen zodat deze de array elementen negeert die uit een html-code bestaat. Om dit goed werkend te krijgen zul je goed moeten weten hoe het programma werkt. Ik denk echter dat dit bij jouw nu niet het geval is. Het is niet even een 'voeg hier deze functie toe en daar een andere en het is klaar'. Ik denk dan ook dat je beter dit idee kunt laten varen, of iemand zoeken die het voor je kan imlpementeren.

Ken Thompson's famous line from V6 UNIX is equaly applicable to this post:
'You are not expected to understand this'


Acties:
  • 0 Henk 'm!

  • radem205
  • Registratie: Juni 2002
  • Laatst online: 02-02-2022
Het zal inderdaad niet makkelijk zijn. Ik ben met deze methode niet helemaal thuis, maar heb ook nog een andere mogelijkheid en dat is met Diff. Is het hierbij makkelijker te realiseren?

Acties:
  • 0 Henk 'm!

  • Janoz
  • Registratie: Oktober 2000
  • Laatst online: 02:21

Janoz

Moderator Devschuur®

!litemod

Daarvoor geldt exact hetzelfde. Je zult de diff zelf aan moeten passen. Als je eerst de opmaak eruit haalt kun je die onmogelijk weer goed in het resultaat terug krijgen. Omdat de tekst immers veranderd is kun je niet meer bepalen waar je tags thuisgehoord zouden hebben.

Wil je deze functionaliteit hebben dan raad ik je aan om niet zomaar de code Jurgle te copy pasten, maar het bijbehorende blog te lezen om te kijken hoe en waarom die methode werkt. Vervolgens zul je lijn voor lijn exact moeten begrijpen wat er in het script gebeurt. Als je dat hebt gedaan kun je hier en daar kleine aanpassingen maken zodat html tags worden genegeerd bij het matchen.

Ken Thompson's famous line from V6 UNIX is equaly applicable to this post:
'You are not expected to understand this'


Acties:
  • 0 Henk 'm!

  • radem205
  • Registratie: Juni 2002
  • Laatst online: 02-02-2022
@Jurgle: Zou je mij hiermee kunnen assisteren? Dat zal ik heel fijn vinden!

Acties:
  • 0 Henk 'm!

  • Jurgle
  • Registratie: Februari 2003
  • Laatst online: 24-06 00:27

Jurgle

100% Compatible

Natuurlijk, maar ik heb zoals iedereen niet heel veel tijd. Je kunt je vragen hier gewoon stellen of ze mailen naar *snip* Dan zal ik mn best doen snel te antwoorden.

Modbreak: Hoe goed bedoeld ook, laten we de discussie gewoon op het forum houden. :)

[ Voor 27% gewijzigd door NMe op 14-05-2007 08:55 ]

My opinions may have changed but not the fact that I am right ― Ashleigh Brilliant


Acties:
  • 0 Henk 'm!

  • radem205
  • Registratie: Juni 2002
  • Laatst online: 02-02-2022
Ik vind het allang fijn dat je er tijd voor wil vrij maken! Bedankt alvast.

Ik heb gelijk al de eerste vraag :). In onderstaande code ga je achterste voren de karakters af en kijk je of de spaties op dezelfde plek staan of klopt dit niet?
Zou je misschien kort kunnen uitleggen wat je hieronder (verder) doet?

PHP:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
         $maxSubCost = 0;
   for($i = 1; $i <= $n; $i++) 
   { 
      $sc = $s[($i - 1)]; 
      for($j = 1; $j <= $m; $j++) 
      { 
         if(preg_replace('/[[:punct:]]/', '', $sc) == preg_replace('/[[:punct:]]/', '', $t[($j - 1)])) // Characters are equal, no cost 
           $subCost = 0; 
         else            // Characters are not equal, costs 1 
           $subCost = 1; 
         
         $left = $d[($i - 1)][$j] + 1;               // left + deletion cost 
         $above = $d[$i][($j - 1)] + 1;              // above + insertion cose 
         $diagonal = $d[($i - 1)][($j - 1)] + $subCost;   // diagonal + substitution cost 
         $d[$i][$j] = min($above, min($left, $diagonal)); // Put minimum in current cell 
         
         if($d[$i][$j] > $maxSubCost)
            $maxSubCost = $d[$i][$j];
      } 
   }


Edit: Nu ik de debug functie op 1 heb gezet wordt het mij al wat meer duidelijk, alleen bovenstaand stukje snap ik niet helemaal.

[ Voor 93% gewijzigd door radem205 op 10-05-2007 19:01 ]


Acties:
  • 0 Henk 'm!

  • radem205
  • Registratie: Juni 2002
  • Laatst online: 02-02-2022
Ik probeer het nu met de DIFF van PEAR om het verschil te laten weergeven, maar dat gaat ook niet helemaal goed.

Met onderstaande code geef ik het verschil weer:
PHP:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
<html><head></head><body>
<?php

include("../config.php");

if(!is_numeric($_GET['sid'])) {
die();
}

$nieuw1=mysql_query("SELECT * FROM documenten_versies WHERE id='".$_GET['sid']."'");
$nieuw=mysql_fetch_assoc($nieuw1);
$vorige1=mysql_query("SELECT tekst FROM documenten_versies WHERE cat='".$nieuw['cat']."' AND id < '".$nieuw['id']."' ORDER BY id DESC LIMIT 0,1");
$vorige=mysql_fetch_assoc($vorige1);

$textnieuw = $nieuw['tekst'];
  $textnieuw = str_replace("\r\n","\n",$textnieuw);
  $textnieuw = str_replace("\r","\n",$textnieuw);


$textvorige = $vorige['tekst'];
  $textvorige = str_replace("\r\n","\n",$textvorige);
  $textvorige = str_replace("\r","\n",$textvorige);


  
      $split = 'characters';
  
  
  require_once("./diff.php");

  $diff = new diff($textvorige,$textnieuw,$split);
  

  
  $diffout = $diff->render();
  
  echo nl2br($diffout);
  
  unset($diff);




echo '<hr>';
echo $textvorige;
echo '<hr>';
echo $textnieuw;
?>
</body></html>


Het volgende geeft ie nu als output: http://www.act-nu.nl/nul-huis/diff/index.php?sid=46
Het eerste blok is de tekst met daarin het verschil aangegeven van onderstaande 2 teksten

Zoals je kan zien gaat het allemaal niet helemaal goed. De html codes wordt soms gesplit en de laatste paar regels horen blauw te zijn, maar doordat tussen de <span> en </span> <p> tags aanwezig zijn wordt dit afgekapt.

Wie kan mij adviseren wat ik het beste kan doen, want weergeven van het verschil tussen 2 documenten is voor mijn project van groot belang.

Alvast heel erg bedankt.

Acties:
  • 0 Henk 'm!

  • Janoz
  • Registratie: Oktober 2000
  • Laatst online: 02:21

Janoz

Moderator Devschuur®

!litemod

Janoz schreef op donderdag 10 mei 2007 @ 15:46:
Ik denk dan ook dat je beter dit idee kunt laten varen, of iemand zoeken die het voor je kan imlpementeren.
En met dat laatste bedoelde ik niet hier iemand ronselen, aangezien dat niet de bedoeling is, maar iemand inhuren die wel de kennis en/of vaardigheden bezit om het te implementeren.

Ken Thompson's famous line from V6 UNIX is equaly applicable to this post:
'You are not expected to understand this'


Acties:
  • 0 Henk 'm!

  • radem205
  • Registratie: Juni 2002
  • Laatst online: 02-02-2022
Wie heeft het hier over "Ronselen". Ik vraag gewoon advies. Daar is dit forum toch voor bedoeld?

En ik vraag netjes aan Jurgie of hij mij zou willen helpen, wat is daar mis mee?

Acties:
  • 0 Henk 'm!

  • Janoz
  • Registratie: Oktober 2000
  • Laatst online: 02:21

Janoz

Moderator Devschuur®

!litemod

Owh, zo bedoelde ik het niet hoor. Met mijn eerdere post wilde ik niet zeggen dat je al aan het ronselen bent, maar wilde ik meer voorkomen dat je zou gaan ronselen. Tot nu toe is je veel hulp aangeboden en heb je ook zelf dingen geprobeerd, maar het is gebleken dat dit specifieke onderdeel je toch boven de pet gaat (met alle respect overigens).

Natuurlijk kun je hier om advies vragen. Daarvoor is dit forum onderandere. Het is hier echter niet de bedoeling dat andere mensen kant en klare oplossingen aan gaan leveren. Het is hier geen 'afhaalchinees' waarbij je een gewenste functionaliteit wenst en andere mensen dit maar even voor je moeten maken. Nu weet ik ook wel dat dat niet jouw insteek is, maar omdat dit onderwerp te lastig is ben ik wel bang dat het daar onbedoeld op uitdraait.

Ken Thompson's famous line from V6 UNIX is equaly applicable to this post:
'You are not expected to understand this'

Pagina: 1