用多个字符串PHP的最后一个字符替换第一个字符 [英] Replace first character with last character of multiple strings PHP

查看:210
本文介绍了用多个字符串PHP的最后一个字符替换第一个字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个代码

I have this code

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

我需要输出为 Doog Reathew

推荐答案

使用下面的代码解决您的目的。
$ b

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:

OUTPUT :

Doog Reathew

这篇关于用多个字符串PHP的最后一个字符替换第一个字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆