Daan Smits
Daan Smits

Reputation: 11

Replacing the first and last letter in a word

I tried to make the first letter replace with the last letter in a word. Here is my code for now. There is a text-area where you can't put text in and the words will be seen under each other. But I could not find a way to make a letter-change QQ

$array = explode(" ", $_POST["text"]);
if ($_POST["submit"])
{

    echo "<pre>";
    foreach ($array as $lijst)
    {
        if (strlen($lijst)>4)
        {
            
            $lijst1= substr_replace($lijst, $lijst[0],-1);
            echo $lijst1;
            echo "<br/>";
        }else{
            echo $lijst;
            echo "<br/>";
        }
    }
    echo "</pre>";
}

Upvotes: 0

Views: 1893

Answers (4)

inxomnyaa
inxomnyaa

Reputation: 99

    <html>
    <body>
        <form method="post">
            <input type="text" name="text">
            <input name="submit" type="hidden" value="true">
            <button>Send</button>
        </form>
<?php
        $array = explode(" ", $_POST["text"]);
        if ($_POST["submit"])// maybe isset()?
        {
            echo "<pre>";
            foreach ($array as $lijst)
            {
                if (strlen($lijst)>4)
                {
                    $char1 = $lijst[0];
                    $char2 = $lijst[strlen($lijst) - 1];
                    $lijst1= $char2. substr($lijst,1,-1) . $char1;
                    echo $lijst1;
                    echo "<br/>";
                }else{
                    echo $lijst;
                    echo "<br/>";
                }
            }
            echo "</pre>";
        }
?>
    </body>
</html>

Upvotes: 0

Murad Hasan
Murad Hasan

Reputation: 9583

Create a function named swaprev() to change the first character with last...

function swaprev($str){
    $str = str_split($str);
    $lc = $str[count($str)-1];
    $fc = $str[0];             
    $str[0] = $lc; $str[count($str)-1] = $fc;
    return implode('',$str);
}

$array = explode(" ", "textarea where you cant put text");
$array_out = [];
foreach($array as $lijst){
    if (strlen($lijst) > 4)
        $array_out[] = swaprev($lijst);
    else
        $array_out[] = $lijst;
}

echo implode(" ", $array_out);

Upvotes: 0

Yuri
Yuri

Reputation: 3284

It is as simple as

$array = explode(" ", $_POST["text"]);
if ($_POST["submit"]) {
    echo "<pre>";
    foreach ($array as $lijst) {
        $lijst1 = $lijst;
        if (strlen($lijst) > 4) {
            $lijst1= $lijst[strlen($lijst)-1].substr($lijst,1,-1).$lijst[0];
        }
        /* Without redundant printing */
        echo $lijst1;
        echo "<br/>";
    }
    echo "</pre>";
}

It just create a new string concatenating: (last char)+(from 2 to n-1 char)+(first char)

Upvotes: 2

Blinkydamo
Blinkydamo

Reputation: 1584

Try this.

        foreach ($array as $lijst)
        {
            if (strlen($lijst)>4)
            {
                $first = $lijst[0];
                $last = $lijst[strlen($lijst)-1];

                $lijst[0] = $last;
                $lijst[strlen($lijst)-1] = $first;

                echo $lijst;
                echo "<br/>";
            }else{
                echo $lijst;
                echo "<br/>";
            }
        }

It will change the first and last in a word that has a string length larger then 4

Upvotes: 0

Related Questions