其他字母都大写...

发布于 2024-11-04 21:58:03 字数 197 浏览 1 评论 0原文

我如何将字符串中的所有其他字母大写?我知道如何转换为较低或较高或第一个等,但不知道如何进行其他操作。为了清楚起见,我举了一些例子。这也是为了创建一个密码,我和我的儿子正在搞乱,我永远不会以这种方式格式化常规文本。

pizza -> PiZzA
party -> PaRtY
popcorn -> PoPcOrN

How would i capitalize every other letter in a string? I know how to convert to lower or upper or the first, etc but not sure how to go about every other. To be clear I've included examples. Also this is for creating a cipher my son and i are messing around with, i would never format regular text in this fashion.

pizza -> PiZzA
party -> PaRtY
popcorn -> PoPcOrN

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

一城柳絮吹成雪 2024-11-11 21:58:03
$newStr = '';
foreach(str_split($str) as $index => $char) {
    $newStr .= ($index % 2) ? strtolower($char) : strtoupper($char);
}

CodePad

$newStr = '';
foreach(str_split($str) as $index => $char) {
    $newStr .= ($index % 2) ? strtolower($char) : strtoupper($char);
}

CodePad.

预谋 2024-11-11 21:58:03

我将尝试这样的事情:

$string = join(
    array_map(
        function($s){
            return ucfirst($s);
        },
        str_split($string,2)
    )
);

或者,作为单行:

$string = join(array_map(function($s){return ucfirst($s);}, str_split($string,2)));

您可以概括该函数,将要大写的块的长度作为参数传递
并让该函数完成工作。该算法很简单:

  • str_split 返回所需长度的字符串数组,
  • array_map 函数将转换添加到块中,并且
  • 连接将字符串粘合在一起形成结果字符串。

最终功能是:

function camelCycles($string, $period) {
  return join(
     array_map(
       function($s){
         return ucfirst($s);
       },
       str_split($string, $period)
     )
  );
}

它仅使用本机构造,因此应该相当快。

编辑:

一个不使用 lambda 的函数,因此适用于 PHP 5.0 及更高版本,因为 str_split 是随 php5 版本引入的

function camelCycles($string, $span) {
  return join(array_map('ucfirst', str_split(strtolower($string), $span)));
}

I will try something like this:

$string = join(
    array_map(
        function($s){
            return ucfirst($s);
        },
        str_split($string,2)
    )
);

or, as one-liner:

$string = join(array_map(function($s){return ucfirst($s);}, str_split($string,2)));

You can generalize the function passing as parameter the length of chunk to be capitalized
and letting the function do the work. The algorithm is straightforward:

  • str_split return an array of strings of the desired length,
  • the array_map function add the transformation to the chunks and
  • the join glues together the strings into the result string.

The final function is:

function camelCycles($string, $period) {
  return join(
     array_map(
       function($s){
         return ucfirst($s);
       },
       str_split($string, $period)
     )
  );
}

It use only native constructs and thus should be quite fast.

EDIT:

A function that doesn't use lambda and therefore suitable for PHP 5.0 and onward, as str_split was introduced with the php5 release

function camelCycles($string, $span) {
  return join(array_map('ucfirst', str_split(strtolower($string), $span)));
}
披肩女神 2024-11-11 21:58:03

我会将其拆分为数组,然后将其重新连接在一起:

function strtoupper_lower($str){
     $temp = explode('',$str);
     $return = '';
     foreach($temp as $i=>$val){
         if($i%1 == 0) $return .= strtolower($val);
         else $return .= strtolower($val);
     } 
     return $return;
}

I would split it as an array and then reattach it together:

function strtoupper_lower($str){
     $temp = explode('',$str);
     $return = '';
     foreach($temp as $i=>$val){
         if($i%1 == 0) $return .= strtolower($val);
         else $return .= strtolower($val);
     } 
     return $return;
}
绝對不後悔。 2024-11-11 21:58:03
function spasticafy($st)
{
    $r = '';
    $n = false;

    foreach(str_split($st) as $i)
    {
        $r .= ($n ? strtoupper($i) : $i);
        $n = !$n;
    }

    return $r;
}

echo spasticafy("hello there, mr person sir");
function spasticafy($st)
{
    $r = '';
    $n = false;

    foreach(str_split($st) as $i)
    {
        $r .= ($n ? strtoupper($i) : $i);
        $n = !$n;
    }

    return $r;
}

echo spasticafy("hello there, mr person sir");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文