加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

PHP如何在复制字符串中的每个字符后“重复”字符串(恢复字符串)

发布时间:2020-12-13 16:59:42 所属栏目:PHP教程 来源:网络整理
导读:嗨,我需要帮助“unduplicating”一个字符串(AKA恢复对字符串的更改).我的 PHP代码中有一个函数,它复制了字符串中的每个字符(“Hello”变成“HHeelllloo”等).现在我想恢复那个,我不知道怎么回事(我想把我的“HHeelllloo”变成“你好”). 这是代码: ?php err
嗨,我需要帮助“unduplicating”一个字符串(AKA恢复对字符串的更改).我的 PHP代码中有一个函数,它复制了字符串中的每个字符(“Hello”变成“HHeelllloo”等).现在我想恢复那个,我不知道怎么回事(我想把我的“HHeelllloo”变成“你好”).

这是代码:

<?php
            error_reporting(-1); // Report all type of errors
            ini_set('display_errors',1); // Display all errors 
            ini_set('output_buffering',0); // Do not buffer outputs,write directly
            ?>

            <!DOCTYPE html>
            <html>
            <head>
            <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
            <title>Untitled 1</title>
            </head>
            <body>
            <?php


            if(isset($_POST["dupe"]) && !empty($_POST["input"])){
                $input = $_POST["input"];
                $newstring = "";

                for($i = 0; $i < strlen($input); $i++){
                    $newstring .= str_repeat(substr($input,$i,1),2);
                }

                echo $newstring;
            }

            if(isset($_POST["undupe"]) && !empty($_POST["input"])){

            }
            ?>

            <form method="post">
                <input type="text" name="input" placeholder="Input"></input><br><br>

                <button type="submit" name="dupe">Dupe</button>
                <button type="submit" name="undupe">Undupe</button>
            </form>

            </body>
            </html>

现在,当我按下“不动”按钮时,我不知道该怎么办. (对不起,如果我在这篇文章中犯了任何错误.我是stackoverflow的新手.)

解决方法

由于字符串排序没有改变,只需遍历字符串并跳过第二个字符:

$undupe = '';
for($i = 0; $i < strlen($duped); $i += 2) {
    $undupe .= $duped[$i]
}

例如

HHeelllloo
0123456789
^ ^ ^ ^ ^
H e l l o
---------
Hello

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读