PHP How to "stretch" a string after duplicating each character in a string (returning a string)

Hey. I need help with an "unuplicating" string (AKA returning changes made to the string). I have a function in my PHP code that duplicates every character in a string ("Hello" becomes "HHeelllloo, etc.). Now I want to return this, and I do not know how (AKA I want to turn my "HHeelllloo" into "Hello").

Here is the code:

<?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>

Now I do not know what to do when I click the "undupe" button. (By the way, sorry if I made any mistakes in this post. Im new to stackoverflow.).

+4
source share
3

, char:

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

.

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

preg_replace reset . .

$str = preg_replace('/.\K./', "", $str);

. . eval.in regex regex101


, , . ^(?:(.)\1)+$

+1

:

$newstring = "";

for($i=0, $size=strlen($input); $i < $size; $i+=2){
    $newstring .= $input[$i];
}

, / $_POST.

0

Source: https://habr.com/ru/post/1630096/


All Articles