1

I have this code

<?php
$str1 = 'Good'
$str2 = 'Weather'
echo $str1, $str2

I need the output as Doog Reathew

2 Answers 2

3

Using the below piece of code solves your purpose. Comments have been added for your understanding.

<?php
$str1 = 'Good';
$str2 = 'Weather';

function swaprev($str1)
{
$str1 = str_split($str1);  //<--- Split the string into separate chars

$lc=$str1[count($str1)-1]; # Grabbing last element
$fe=$str1[0];              # Grabbing first element
$str1[0]=$lc;$str1[count($str1)-1]=$fe;    # Do the interchanging
return $str1 = implode('',$str1);  # Recreate the string
}

echo ucfirst((strtolower(swaprev($str1))))." ".ucfirst((strtolower(swaprev($str2))));

OUTPUT :

Doog Reathew
Sign up to request clarification or add additional context in comments.

3 Comments

@DummyCode, I have used ucfirst() ;) see the code closely.
how about reathew? is that possible? i only wanted the first letter and last but the mod edited it lomo.
@user3487504 remove the ucfirst then. See, php.net/manual/en/function.ucfirst.php
0

just write below function ,it will work

function replace($string) {

$a = substr($string, 0, 1);
$b = substr($string, -1);
$string = $b . (substr($string, 1, strlen($string)));
$string = substr($string, 0, strlen($string) - 1);
$string = $string . $a;

return ucfirst(strtolower($string));

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.