| PHP Developer Forum Hier habt ihr die Möglichkeit, eure Skriptprobleme mit anderen Anwendern zu diskutieren. Seid so fair und beantwortet auch Fragen von anderen Anwendern. Dieses Forum ist sowohl für ANFÄNGER als auch für PHP-Profis! Post your PHP questions here! |
 |
|

27-11-2002, 12:26
|
TBT
 Moderator
|
|
Registriert seit: Sep 2002
Ort: Berlin
Beiträge: 2.776
|
|
Knobelaufgabe ?
Hat jemand eine Idee, wie man effizient die Unterschiede zwischen 2 Strings ermittlen kann?
Ich möchte die Unterschiede farblich hervorheben, und zwar am liebsten in der Art, daß
die Teile die im ersten String, aber nicht im zweiten sind grün markiert werden,
und die Teile die im zweiten aber nicht im ersten sind rot.
Also so:
Code:
dies ist der 1. Text blabla
Code:
dies ist der 2. Text hallo
es würde mir auch schon reichen, wenn er mir anzeigt, was sich beim zweiten gegenüber
dem ersten geändert hat.
Jede Idee willkommen
|

27-11-2002, 13:51
|
|
MoRtAlAn
PHP Master
|
|
Registriert seit: Jan 2002
Ort: Mühlheim am Main
Beiträge: 5.934
|
|
versuch mal das, das sollte auch gehen (allerdings nur für 2 Strings!):
PHP-Code:
<?php
/**
* Stringvergleich
*
* Vergleicht 2 Strings und gibt Wortunterschiede farbig aus
*
* @author Marco Jahn <mortalan@php-resource.de>
*/
/**
* Vergleicht die Strings
*
* @var string $string1 Ausgangssatz (welche Wörter kommen nicht in $string2 vor)
* @var string $string2 Vergleichssatz
* @var string $color Farbwert für nicht gefundene Worte
*/
function stringVergleich($string1,$string2,$color) {
$as1 = explode(" ",$string1);
$as2 = explode(" ",$string2);
$ii = count($as1);
$i = 0;
do {
if (!in_array($as1[$i],$as2)) {
$nas1[$i] = "<font color=\"".$color."\">".$as1[$i]."</font>";
} else {
$nas1[$i] = $as1[$i];
}
$i++;
} while ($i < $ii);
$fs = join(" ",$nas1);
return $fs;
}
$satz1 = "dies ist der 1. Text blabla";
$satz2 = "dies ist der 2. Text hallo bla";
echo stringVergleich($string1 = $satz1,$string2 = $satz2, $color = "#ff0000");
echo "<br>";
echo stringVergleich($string1 = $satz2,$string2 = $satz1, $color = "teal");
?>
|

27-11-2002, 14:03
|
TBT
 Moderator
|
|
Registriert seit: Sep 2002
Ort: Berlin
Beiträge: 2.776
|
|
@mortalan nicht schlecht, aber sorry, nicht ganz
PHP-Code:
function stringVergleich($string1,$string2,$color) {
$as1 = explode(" ",$string1);
$as2 = explode(" ",$string2);
$ii = count($as1);
$i = 0;
do {
if (!in_array($as1[$i],$as2)) {
$nas1[$i] = "<font color=\"".$color."\">".$as1[$i]."</font>";
} else {
$nas1[$i] = $as1[$i];
}
$i++;
} while ($i < $ii);
$fs = join(" ",$nas1);
return $fs;
}
$text1="Software & Support Verlag GmbH is going to publish an International version of the PHP Magazin. This magazine was
initiated after growing interest for an English magazine after the German version has been around for a few months. PHP
Magazine not only informs about the scripting language itself, but also about related technologies such as the Apache Web
Server, database blabla technologies, XML and other innovative internet technologies. Different sections within the magazine
are oriented towards the specific question areas with which a web developer is confronted in daily practice. The first issue
will be published in December 31 and the frequency of issues is two months. You will be able to subscribe on the website which
will open shortly.";
$text2="Software & Support Verlag GmbH is going to publish an International version of the PHP Magazin. This magazine was
initiated after growing interest for an English magazine after the German version has been around for a few months. PHP
Magazine not only informs you about the scripting language itself, but also about related technologies such as the Apache Web
Server, database technologies, XML1 and other innovative internet technologies. Different sections within the magazine are
oriented towards the specific questions areas 51 with you can which a web developer is confronted in daily practice. The first
issue will be published in December and the frequency of issues is two months. You will be able to subscribe on the website
which will open shortly.";
echo stringVergleich($text1,$text2, $color = "#ff0000");
echo "<br><br><br>";
echo stringVergleich($text2,$text1, $color = "teal");
ergibt:
Code:
Software & Support Verlag GmbH is going to publish an International version of the PHP Magazin. This magazine was
initiated after growing interest for an English magazine after the German version has been around for a few months. PHP
Magazine not only informs about the scripting language itself, but also about related technologies such as the Apache Web
Server, database blabla technologies, XML and other innovative internet technologies.
Different sections within the magazine are oriented towards the specific
question areas with which a web developer is confronted in daily practice. The first issue
will be published in December 31 and the frequency of issues is two months. You will be able to
subscribe on the website which will open shortly.
Software & Support Verlag GmbH is going to
publish an International version of the PHP Magazin. This magazine was
initiated after growing interest for an English magazine after the German version has been around for a few months. PHP
Magazine not only informs you about the scripting language itself, but also about related technologies such
as the Apache Web Server, database technologies, XML1 and other innovative internet technologies. Different
sections within the magazine are oriented towards the specific questions
areas 51 with you can which a web developer is confronted in
daily practice. The first issue will be published in December and the frequency of issues is two months.
You will be able to subscribe on the website
which will open shortly.
noch 5 min, hab da auch gleich was fertig
|

27-11-2002, 14:05
|
|
MoRtAlAn
PHP Master
|
|
Registriert seit: Jan 2002
Ort: Mühlheim am Main
Beiträge: 5.934
|
|
mh...
noch nen trim() um überflüssige leerzeichen rauszunehmen...
soll question und questions (beispiel) auch gemeldet werden oder nicht ??
wenn nicht, mal mit soundex probieren!
außerdem, besser als nichts
gruss
|

27-11-2002, 14:16
|
TBT
 Moderator
|
|
Registriert seit: Sep 2002
Ort: Berlin
Beiträge: 2.776
|
|
hab jetzt das hier
PHP-Code:
$text1="Software & Support Verlag GmbH is going to publish an International version of the PHP Magazin. This magazine was
initiated after growing interest for an English magazine after the German version has been around for a few months. PHP
Magazine not only informs about the scripting language itself, but also about related technologies such as the Apache Web
Server, database blabla technologies, XML and other innovative internet technologies. Different sections within the magazine
are oriented towards the specific question areas with which a web developer is confronted in daily practice. The first issue
will be published in December 31 and the frequency of issues is two months. You will be able to subscribe on the website which
will open shortly.";
$text2="Software & Support Verlag GmbH is going to publish an International version of the PHP Magazin. This magazine was
initiated after growing interest for an English magazine after the German version has been around for a few months. PHP
Magazine not only informs you about the scripting language itself, but also about related technologies such as the Apache Web
Server, database technologies, XML1 and other innovative internet technologies. Different sections within the magazine are
oriented towards the specific questions areas 51 with you can which a web developer is confronted in daily practice. The first
issue will be published in December and the frequency of issues is two months. You will be able to subscribe on the website
which will open shortly.";
$array1=explode(" ",str_replace(array(" ","\\r","\\n"),array(" ","",""),$text1));
$array2=explode(" ",str_replace(array(" ","\\r","\\n"),array(" ","",""),$text2));
while(list($key1,$value1)=each($array1)){
reset($array2);
$i=$startfrom=0;
while(list($key2,$value2)=each($array2)){
if($key2<$startfrom)continue;
if($value1==$value2){
unset($array1[$key1],$array2[$key2]);
$startfrom=$key2;
break;
}
}
}
$safe1=explode(" ",$text1);
reset($array1);
while(list($key1,)=each($array1))
$safe1[$key1]="<font color=green>".$safe1[$key1]."</font>";
$safe2=explode(" ",$text2);
reset($array2);
while(list($key2,)=each($array2))
$safe2[$key2]="<font color=red>".$safe2[$key2]."</font>";
echo implode(" ",$safe1)."<br><br><br>".implode(" ",$safe2);
ergibt:
Code:
Software & Support Verlag GmbH is going to publish an International version of the PHP Magazin. This magazine was
initiated after growing interest for an English magazine after the German version has been around for a few months. PHP
Magazine not only informs about the scripting language itself, but also about related technologies such as the Apache Web
Server, database blabla technologies, XML and other innovative internet technologies.
Different sections within the magazine are oriented towards the specific question areas with which a web
developer is confronted in daily practice. The first issue will be published in December 31 and the frequency of issues is two months.
You will be able to subscribe on the website which will open shortly.
Software & Support Verlag GmbH is going to publish an International version of the PHP Magazin. This magazine was
initiated after growing interest for an English magazine after the German version has been around for a few months. PHP
Magazine not only informs you about the scripting language itself, but also about related technologies such as the Apache Web
Server, database technologies, XML1 and other innovative internet technologies.
Different sections within the magazine are oriented towards the specific questions areas 51 with you can which a web
developer is confronted in daily practice. The first issue will be published in December and the frequency of issues is two months.
You will be able to subscribe on the website which will open shortly.
aber bei anderen String klappt es auch nicht 100%-ig
|

27-11-2002, 14:20
|
|
MoRtAlAn
PHP Master
|
|
Registriert seit: Jan 2002
Ort: Mühlheim am Main
Beiträge: 5.934
|
|
mh... der matcht allerdings ein bisschen besser so...
mh... hast du ne ahnung, wie man den miteinbauen könnte ?? levenshtein
|

27-11-2002, 14:44
|
TBT
 Moderator
|
|
Registriert seit: Sep 2002
Ort: Berlin
Beiträge: 2.776
|
|
so noch nen bissel optimiert,
läuft jetzt 5-6mal schneller als meine obere Variante
PHP-Code:
$array1=explode(" ",str_replace(array(" ","\r","\n"),array(" ","",""),$text1));
$array2=explode(" ",str_replace(array(" ","\r","\n"),array(" ","",""),$text2));
$max1=count($array1);
$max2=count($array2);
$startfrom=0;
for($i1=0;$i1<$max1;++$i1){
for($i2=$startfrom;$i2<$max2;++$i2){
if($array1[$i1]==$array2[$i2]){
unset($array1[$i1],$array2[$i2]);
$startfrom=$i2+1;
break;
}
}
}
$safe1=explode(" ",$text1);
reset($array1);
while(list($key1,)=each($array1))
$safe1[$key1]="<font color=green>".$safe1[$key1]."</font>";
$safe2=explode(" ",$text2);
reset($array2);
while(list($key2,)=each($array2))
$safe2[$key2]="<font color=red>".$safe2[$key2]."</font>";
echo implode(" ",$safe1)."<br><br><br>".implode(" ",$safe2)."<br><br>";
|

28-11-2002, 11:16
|
TBT
 Moderator
|
|
Registriert seit: Sep 2002
Ort: Berlin
Beiträge: 2.776
|
|
Achtung!
der Algorythmus hat einen ganz großen Hacken.
Er versagt total bei folgender Beispielkonstellation
PHP-Code:
$text1="Hallo Dies ist ein Text und kein einzelnes Wort";
$text2="Dies ist ein Text und kein einzelnes Wort Hallo";
|

28-11-2002, 11:26
|
|
MoRtAlAn
PHP Master
|
|
Registriert seit: Jan 2002
Ort: Mühlheim am Main
Beiträge: 5.934
|
|
logisch, er überprüft ja nicht den "standort" des Wortes!
mh...
man müßte folgendes Einbauen:
- Vergleich der Position des Wortes
- ist ein Wort nicht vorhanden, muss es markiert werden, darf allerdings nicht mitgezählt werden, da sonst alle folgenden Wörter gekennzeichnet werden
|

28-11-2002, 11:35
|
TBT
 Moderator
|
|
Registriert seit: Sep 2002
Ort: Berlin
Beiträge: 2.776
|
|
ich hab da ein einen "schaukelnden" Algorythmus gedacht.
dh.
als erstes wird die Postition des Wortes aus text1 in text2 gesucht.
ist dies nicht das nächste Wort (also gleicher Text) wird die Gegenprobe
gemacht. dh das aktuelle Wort aus text2 in text1 gesucht.
Der kleinere Abstand von beiden wird dann als Ergebniss zum weiterarbeiten genutzt.
|

28-11-2002, 11:37
|
|
MoRtAlAn
PHP Master
|
|
Registriert seit: Jan 2002
Ort: Mühlheim am Main
Beiträge: 5.934
|
|
mh.. auchne idee, müßte man mal testen, was mehr Performance braucht ?!
|

28-11-2002, 13:45
|
|
Troublegum
PHP Senior
|
|
Registriert seit: Dec 2001
Beiträge: 1.302
|
|
PS: es heisst Algorithmus, nicht Algorythmus.
__________________
"Los, lass uns loslegen! Hm ? Quatschen können wir hinterher immer noch!"
"Aber Bommel, wir können jetzt nicht bumsen. Wir müssen doch erst den Kindern - ... "
"Ja ja ja. Du willst immer nur das Eine. Buchstabenzeigen, Buchstabenzeigen - meine Gefühle sind dir wohl scheißegal."
© Harald Schmidt
|

28-11-2002, 16:07
|
TBT
 Moderator
|
|
Registriert seit: Sep 2002
Ort: Berlin
Beiträge: 2.776
|
|
Zitat:
Original geschrieben von Troublegum
PS: es heisst Algorithmus, nicht Algorythmus.
|
bei mir heißt es Algo Rythmus, weil er schaukelt ja
|

28-11-2002, 17:20
|
TBT
 Moderator
|
|
Registriert seit: Sep 2002
Ort: Berlin
Beiträge: 2.776
|
|
schaukel, schaukel, schaukel
PHP-Code:
$array1 = explode(" ", str_replace(array(" ", "\r", "\n"), array(" ", "", ""), $text1));
$array2 = explode(" ", str_replace(array(" ", "\r", "\n"), array(" ", "", ""), $text2));
$max1 = count($array1);
$max2 = count($array2);
$start1 = $start2 = 0;
$jump1 = $jump2 = 0;
while($start1 < $max1 && $start2 < $max2){
$pos11 = $pos12 = $start1;
$pos21 = $pos22 = $start2;
$diff2 = 0;
// schaukel 1. Array hoch
while($pos11 < $max1 && $array1[$pos11] != $array2[$pos21]){
++$pos11;
}
// Ende des 1 Arrays erreicht ?
if($pos11 == $max1){
$start2++;
continue;
}
// Gegenschaukel wenn übersprunge Wörter
if(($diff1 = $pos11 - $pos21) > 1){
while($pos22 < $max2 && $array1[$pos12] != $array2[$pos22]){
++$pos22;
}
$diff2 = $pos22 - $pos12 + $jump2;
}
// Ende des 2 Arrays erreicht ?
if($pos22 == $max2){
$start1++;
continue;
}
$diff1 += $jump1;
// Auswertung der Schaukel
if($diff1 >= $diff2 && $diff2){
unset($array1[$pos12], $array2[$pos22]);
$start1 = $pos12 + 1;
$start2 = $pos22 + 1;
$jump2 = $diff2;
}else{
unset($array1[$pos11], $array2[$pos21]);
$start1 = $pos11 + 1;
$start2 = $pos21 + 1;
$jump1 = $diff1;
}
}
$safe1 = explode(" ", $text1);
reset($array1);
while(list($key1,) = each($array1)){
$safe1[$key1] = "<font color=green>" . $safe1[$key1] . "</font>";
}
$safe2 = explode(" ", $text2);
reset($array2);
while(list($key2,) = each($array2)){
$safe2[$key2] = "<font color=red>" . $safe2[$key2] . "</font>";
}
echo implode(" ", $safe1) . "<br><br><br>" . implode(" ", $safe2) . "<br><br>";
habe bis jetzt keinen Text gefunden, bei dem nicht sinnvoll markiert wurde
|
|
Aktive Benutzer in diesem Thema: 1 (Registrierte Benutzer: 0, Gäste: 1)
|
|
|
| Themen-Optionen |
|
|
| Thema bewerten |
|
|
Forumregeln
|
Es ist Ihnen nicht erlaubt, neue Themen zu verfassen.
Es ist Ihnen nicht erlaubt, auf Beiträge zu antworten.
Es ist Ihnen nicht erlaubt, Anhänge hochzuladen.
Es ist Ihnen nicht erlaubt, Ihre Beiträge zu bearbeiten.
HTML-Code ist aus.
|
|
|
|
PHP News
|