php - 如何反转字符串的两个字符?
问题描述
我想在 PHP 中反转字符串的两个字符。例如50378f
请8f3750
帮助我。
$str= User::where('id',$userid)->pluck('card_id');
$num = strrev($number);
echo $num;
这个功能反转很好,但我想反转两个字符而不是一个字符。
我的功能是给我输出示例:12345
to543210
但我希望它像
103254
.
解决方案
你可以试试这个:
$originalString = '23242526';
$arrayWith2CharsPerElement = str_split($originalString, 2);
$arrayWithReversedKeys = array_reverse($arrayWith2CharsPerElement);
$newStringInReverseOrder = implode($arrayWithReversedKeys);
echo $newStringInReverseOrder; //will print 26252423
编辑:更改了使用奇数字符串的方法
$string = '121314152';
$countDown = strlen($string);
$substrLength = 2;
$reverseString = '';
while ($countDown > 0) {
$startPosition = $countDown -2;
if ($countDown == 1) {
$startPosition = 0;
$substrLength = 1;
}
$reverseString .= substr($string, $startPosition, $substrLength);
$countDown -= 2;
}
echo $reverseString; //will print 524131211
推荐阅读
- css - CSS 覆盖或继承
- python - 尝试在 emacs 中安装 python-pycompile
- spring - rabbitmq convertAndSend 导致线程在写入关闭的连接时挂起
- regex - 如何从某事物中选择正则表达式,除了 \(反斜杠)
- animation - 偏移量滞后的动画
- android - 不希望 Deep Link 重定向到浏览器
- c# - 带有 JSON 和文件的 C# API Web 请求
- jquery - 如何获取输入的名称或ID?
- azure - Get-AzConsumptionBudget 显示不正确/不完整的数据
- flutter - Flutter GetX 状态变化时触发函数