重新加载以前在字符串

时间:2015-11-06 04:20:12

标签: php arrays string replace

我手动交换字符串中的字母对,它只能工作一次,如果我尝试更改第二对,之前交换的对将被换回,我如何重新加载以前进行交换的字符串?

这是我的代码:

 <?php

        function manual($letter1, $letter2, $text) {
            $changed = str_split($text);
            for ($i = 0; $i < count( $changed); $i++) {
                if ( $changed[$i] == $letter1) {
                     $changed[$i] = $letter2;
                } elseif ( $changed[$i] == $letter2) {
                     $changed[$i] = $letter1;
                }
            }
            return  $changed;
        }
?>

<form action="index.php" method="post">
            <p>Letra 1: <input type="text" name="firstletter" /></p>
            <p>Letra 2: <input type="text" name="secondletter" /></p>
            <input type="submit" name="submit" value="Submit" />
        </form>

<?php
if (isset($_POST['firstletter']) && isset($_POST['secondletter'])) {
    $ch = $_POST['firstletter'];
    echo $ch . "<br />";
    $ch2 = $_POST['secondletter'];
    echo $ch2;
    $final = manual($ch, $ch2, $final);
    echo "<pre><br />" . implode("", $final) . "</pre>";
}
    ?>

该计划的整个代码是:

        <?php

        function manual($letter1, $letter2, $text) {
            $changed = str_split($text);
            for ($i = 0; $i < count( $changed); $i++) {
                if ( $changed[$i] == $letter1) {
                     $changed[$i] = $letter2;
                } elseif ( $changed[$i] == $letter2) {
                     $changed[$i] = $letter1;
                }
            }
            return  $changed;
        }

        $reference = file_get_contents('/textfiles/reference_file.txt', true);
        $encrypted = file_get_contents('/textfiles/encrypted_file.txt', true);

        $azreference = preg_replace("/[^a-zA-Z]+/", "", $reference);
        $azencrypted = preg_replace("/[^a-zA-Z]+/", "", $encrypted);

        $refarray1 = array_count_values(str_split($azreference, '1'));
        $refarray2 = array_count_values(str_split($azencrypted, '1'));
        $replacement = array();

        foreach ($refarray1 as $key => $val) {
            foreach ($refarray2 as $key2 => $val2) {
                if ($val == $val2) {
                    $replacement[$key2] = $key;
                }
            }
        }

        $final = strtr($encrypted, $replacement);
        echo "<pre>" . $final . "</pre>";
        echo "<br />";
    ?>
    <form action="index.php" method="post">
            <p>Letra 1: <input type="text" name="firstletter" /></p>
            <p>Letra 2: <input type="text" name="secondletter" /></p>
            <input type="submit" name="submit" value="Submit" />
        </form>

 <?php
            if (isset($_POST['firstletter']) && isset($_POST['secondletter'])) {
                $ch = $_POST['firstletter'];
                $ch2 = $_POST['secondletter'];
                if (file_exists('C:/xampp/htdocs/ictfinal/file.txt')) {
                    $finalfile = 'file.txt';
                    $handle = fopen($finalfile, 'r+') or die('Cannot open file:  ' . $my_file);
                    $file1 = file_get_contents('/file.txt', true);
                    $final = manual($ch, $ch2, $file1);

                    $data = implode("", $final);
                    fwrite($handle, $data);
                } else {
                    $final = manual($ch, $ch2, $final);
                    $finalfile = 'file.txt';
                    $handle = fopen($finalfile, 'w+') or die('Cannot open file:  ' . $my_file);
                    $data = implode("", $final);
                    file_put_contents('C:/xampp/htdocs/ictfinal/file.txt', $data);
                }
                ?>

0 个答案:

没有答案