如何在 php 中大写所有其他字符?

发布于 2024-12-01 06:22:33 字数 246 浏览 2 评论 0 原文

我想在 php 中使用 CaPiTaLiZe $string,不要问为什么:D

我做了一些研究并在这里找到了很好的答案,他们真的帮助了我。 但是,就我而言,我想开始将每个单词中的每个奇怪字符(1,2,3...)大写。

例如,通过我的自定义函数,我得到这个结果“TeSt eXaMpLe”,并且想要得到这个“TeSt ExAmPlE”。 看到第二个例子中单词“example”以大写“E”开头了吗?

那么,有人可以帮助我吗? :)

I want to CaPiTaLiZe $string in php, don't ask why :D

I made some research and found good answers here, they really helped me.
But, in my case I want to start capitalizing every odd character (1,2,3...) in EVERY word.

For example, with my custom function i'm getting this result "TeSt eXaMpLe" and want to getting this "TeSt ExAmPlE".
See that in second example word "example" starts with capital "E"?

So, can anyone help me? : )

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

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

发布评论

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

评论(5

不交电费瞎发啥光 2024-12-08 06:22:33

好吧,我会把它变成一个数组,然后再把它放回去。

<?php

$str = "test example";

$str_implode = str_split($str);

$caps = true;
foreach($str_implode as $key=>$letter){
    if($caps){
        $out = strtoupper($letter);
        if($out <> " ") //not a space character
            $caps = false;
    }
    else{
        $out = strtolower($letter);
        $caps = true;
    }
    $str_implode[$key] = $out;
}

$str = implode('',$str_implode);

echo $str;

?>

演示:http://codepad.org/j8uXM97o

Well I would just make it an array and then put it back together again.

<?php

$str = "test example";

$str_implode = str_split($str);

$caps = true;
foreach($str_implode as $key=>$letter){
    if($caps){
        $out = strtoupper($letter);
        if($out <> " ") //not a space character
            $caps = false;
    }
    else{
        $out = strtolower($letter);
        $caps = true;
    }
    $str_implode[$key] = $out;
}

$str = implode('',$str_implode);

echo $str;

?>

Demo: http://codepad.org/j8uXM97o

安稳善良 2024-12-08 06:22:33

我会使用正则表达式来执行此操作,因为它简洁且易于执行:

$str = 'I made some research and found good answers here, they really helped me.';
$str = preg_replace_callback('/(\w)(.?)/', 'altcase', $str);
echo $str;

function altcase($m){
    return strtoupper($m[1]).$m[2];
}

输出:“我做了一些研究并在这里找到了好的答案,他们真的帮助了我。”

示例

I would use regex to do this, since it is concise and easy to do:

$str = 'I made some research and found good answers here, they really helped me.';
$str = preg_replace_callback('/(\w)(.?)/', 'altcase', $str);
echo $str;

function altcase($m){
    return strtoupper($m[1]).$m[2];
}

Outputs: "I MaDe SoMe ReSeArCh AnD FoUnD GoOd AnSwErS HeRe, ThEy ReAlLy HeLpEd Me."

Example

吻安 2024-12-08 06:22:33

这是一款应该可以使用的内衬。

preg_replace('/(\w)(.)?/e', "strtoupper('$1').strtolower('$2')", 'test example');

http://codepad.org/9LC3SzjC

Here's a one liner that should work.

preg_replace('/(\w)(.)?/e', "strtoupper('$1').strtolower('$2')", 'test example');

http://codepad.org/9LC3SzjC

暖阳 2024-12-08 06:22:33

尝试:

function capitalize($string){
    $return= "";
    foreach(explode(" ",$string) as $w){
        foreach(str_split($w) as $k=>$v) {
            if(($k+1)%2!=0 && ctype_alpha($v)){
                $return .= mb_strtoupper($v);
            }else{
                $return .= $v;
            }
        }
        $return .= " ";
    }
    return $return;
}
echo capitalize("I want to CaPiTaLiZe string in php, don't ask why :D");
//I WaNt To CaPiTaLiZe StRiNg In PhP, DoN'T AsK WhY :D

编辑:修复了输出中缺少特殊字符的问题。

Try:

function capitalize($string){
    $return= "";
    foreach(explode(" ",$string) as $w){
        foreach(str_split($w) as $k=>$v) {
            if(($k+1)%2!=0 && ctype_alpha($v)){
                $return .= mb_strtoupper($v);
            }else{
                $return .= $v;
            }
        }
        $return .= " ";
    }
    return $return;
}
echo capitalize("I want to CaPiTaLiZe string in php, don't ask why :D");
//I WaNt To CaPiTaLiZe StRiNg In PhP, DoN'T AsK WhY :D

Edited: Fixed the lack of special characters in the output.

遇见了你 2024-12-08 06:22:33

此任务可以在不使用捕获组的情况下执行 - 只需使用 ucfirst() 即可。

这不是为处理多字节字符而构建的。

抓取一个单词字符,然后(可选)抓取下一个字符。从全字符串匹配中,仅更改第一个字符的大小写。

代码:(演示)(或演示

$strings = [
    "test string",
    "lado lomidze needs a solution",
    "I made some research and found 'good' answers here; they really helped me."
];  // if not already all lowercase, use strtolower()

var_export(preg_replace_callback('/\w.?/', function ($m) { return ucfirst($m[0]); }, $strings));

输出:

array (
  0 => 'TeSt StRiNg',
  1 => 'LaDo LoMiDzE NeEdS A SoLuTiOn',
  2 => 'I MaDe SoMe ReSeArCh AnD FoUnD \'GoOd\' AnSwErS HeRe; ThEy ReAlLy HeLpEd Me.',
)

对于其他研究人员,如果您(更简单地)只想将所有其他字符转换为大写,您可以使用/..?/ 在您的模式中,但在这种情况下使用正则表达式会有点过分。您可以更有效地使用 for() 循环和双增量。

代码(演示

$string = "test string";
for ($i = 0, $len = strlen($string); $i < $len; $i += 2) {
    $string[$i] = strtoupper($string[$i]);
}
echo $string;
// TeSt sTrInG
// ^-^-^-^-^-^-- strtoupper() was called here

This task can be performed without using capture groups -- just use ucfirst().

This is not built to process multibyte characters.

Grab a word character then, optionally, the next character. From the fullstring match, only change the case of the first character.

Code: (Demo) (or Demo)

$strings = [
    "test string",
    "lado lomidze needs a solution",
    "I made some research and found 'good' answers here; they really helped me."
];  // if not already all lowercase, use strtolower()

var_export(preg_replace_callback('/\w.?/', function ($m) { return ucfirst($m[0]); }, $strings));

Output:

array (
  0 => 'TeSt StRiNg',
  1 => 'LaDo LoMiDzE NeEdS A SoLuTiOn',
  2 => 'I MaDe SoMe ReSeArCh AnD FoUnD \'GoOd\' AnSwErS HeRe; ThEy ReAlLy HeLpEd Me.',
)

For other researchers, if you (more simply) just want to convert every other character to uppercase, you could use /..?/ in your pattern, but using regex for this case would be overkill. You could more efficiently use a for() loop and double-incrementation.

Code (Demo)

$string = "test string";
for ($i = 0, $len = strlen($string); $i < $len; $i += 2) {
    $string[$i] = strtoupper($string[$i]);
}
echo $string;
// TeSt sTrInG
// ^-^-^-^-^-^-- strtoupper() was called here
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文