STR_Replace + Array ... falsch replaced

Einklappen
X
 
  • Filter
  • Zeit
  • Anzeigen
Alles löschen
neue Beiträge

  • STR_Replace + Array ... falsch replaced

    Hay Leutz...

    hab ein kleines Problemchen mit dem befehl str_replace und der zusätzlichen nutzung von Arrays....
    ScriptA=test.php
    ScriptB=test2.php

    Das besagte ScriptA soll einen Text ""verschlüsseln"" und ScriptB soll das ganze wieder ""entschlüsseln"".

    Aber schon ScriptA arbeitet nicht richtig...

    Die Ausgabe müsste so aussehen,
    Iotk_tof_ytby7894561230

    sieht aber so aus,
    Ggnk_ngf_nnbn2301111230

    Wad hab ich falsch gemacht?
    Oder war die grundüberlegung falsch?


    Greeze Jaydabi


    EDIT: PHP4

    test.php
    PHP-Code:
    <?php
    $text
    ="Hier ein text0123456789";

    $abc=array("a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z");
    $ABC=array("A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z");
    $cba=array("q","w","e","r","t","z","u","i","o","p","a","s","d","f","g","h","j","k","l","y","x","c","v","b","n","m");
    $CBA=array("Q","W","E","R","T","Z","U","I","O","P","A","S","D","F","G","H","J","K","L","Y","X","C","V","B","N","M");
    $ezd=array("0","1","2","3","4","5","6","7","8","9");
    $dze=array("7","8","9","4","5","6","1","2","3","0");

    $text=str_replace($abc,$cba,$text);
    $text=str_replace($ABC,$CBA,$text);
    $text=str_replace($ezd,$dze,$text);

    $text=str_replace(" ","_",$text);

    echo 
    $text;
    ?>



    test2.php
    PHP-Code:
    <?php
    header
    ("Content-type: text/html");
    $i=$_GET["i"];

    $i=str_replace("_"," ",$i);
    $i=str_replace("-@-","<br>",$i);

    $abc=array("a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z");
    $ABC=array("A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z");
    $cba=array("q","w","e","r","t","z","u","i","o","p","a","s","d","f","g","h","j","k","l","y","x","c","v","b","n","m");
    $CBA=array("Q","W","E","R","T","Z","U","I","O","P","A","S","D","F","G","H","J","K","L","Y","X","C","V","B","N","M");
    $ezd=array("0","1","2","3","4","5","6","7","8","9");
    $dze=array("7","8","9","4","5","6","1","2","3","0");

    $i=str_replace($dze,$ezd,$i);
    $i=str_replace($CBA,$ABC,$i);
    $i=str_replace($cba,$abc,$i);

    echo 
    $i;
    ?>

  • #2
    Mal ganz unabhängig von deiner Frage, du solltest dein System nochmal Gründlich überdenken, denn:

    Du ersetzt verschiedene Zeichen mehrmals, das kann ja nur schiefgehen. Wenn du jetzt also was verschlüsselst, und dort dann ein anderes zeichen steht, dann kann wenn du pech hast beim entschlüsseln ein ganz anderes zeichen rauskommen als du ursprünglich eingegeben hast.

    Ach... halt... stop *denk* ...

    Achse nee, du hast die Arrays einfach umgedreht, und glaubst es damit rückgängig zu ersetzen... ne dis geht eh nich. Lass die Arrays mal richtig rum und probiers nochmal. vielleicht hilfts.

    Gruß

    ArSeN
    Nur wenige wissen, wieviel man wissen muss, um zu wissen, wie wenig man weiß.

    Kommentar


    • #3
      Stimmt... mist... a wird zu q und q wird ja nochmal replaced ..... ok dann hat sich das schon erledigt xD shid...

      naja egal !


      ThX

      Kommentar


      • #4
        Das H in $ABC wird durch das I aus $CBA ersetzt, aber...

        Das jetzt "neue" I wird wieder in $AC gefunden und durch ein O (aus $CBA) ersetzt, das O aus $ABC wird dann durch ein G aus $CBA ersetzt
        EDIT:
        Interne Notiz: Nicht ans Telefon gehen, wenn's klingelt
        Ich denke, also bin ich. - Einige sind trotzdem...

        Kommentar


        • #5
          Gibts ne möglichkeit die schon ersetzten zeichen vor einem erneuten ersetzen zu schützen?

          EDIT:
          Telefon? Was für ein Telefon? xD

          Kommentar


          • #6
            Original geschrieben von Jaydabi
            Gibts ne möglichkeit die schon ersetzten zeichen vor einem erneuten ersetzen zu schützen?[/EDIT]
            Sicher, wenn du's manuell machst ... merke dir in einem Array, welche Zeichen du bereits ersetzt hast.

            Pseudocode:
            Code:
            $bereitsErsetzt = array();
            Schleife über $sucharray {
               Schleife über alle Zeichen in $string mit Zähler $i {
                  wenn $bereitsErsetzt[$i] noch nicht gesetzt ist {
                     ersetze das Zeichen $string[$i];
                     $bereitsErsetzt[$i] = true;
                  }
               }
            }
            I don't believe in rebirth. Actually, I never did in my whole lives.

            Kommentar

            Lädt...
            X