将所有大写字母转换为首字母大写

发布于 2024-08-18 17:41:08 字数 218 浏览 5 评论 0原文

我想知道是否有任何方法仅使用CSS(这可以是浏览器特定的CSS)将所有大写文本转换为仅最初大写的单词,例如:

I YELL WITH MY KEYBOARD

将是转换为:

我用键盘大喊

编辑:我意识到我的问题不够清楚,输入的文本是大写的,而不是最初小写的,文本转换:大写不适用于数据看起来是这样输入的。

I was wondering if there was any way using only CSS, (and this CAN be browser specific CSS) to convert all uppercase text to words that are only initially capitalized for example:

I YELL WITH MY KEYBOARD

would be converted to:

I Yell With My Keyboard

EDIT: I realized I wasn't clear enough in my question, the text was entered capitalized, not initially lowercase, text-transform: capitalize doesn't work on data that has been entered like this it seems.

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

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

发布评论

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

评论(1

月野兔 2024-08-25 17:41:08

您可以使用 CSS 进行一些文本转换操作,以下是所有这些操作。

.Capitalize
{ text-transform: capitalize }

.Uppercase
{ text-transform: uppercase }

.Lowercase
{ text-transform: lowercase }

.Nothing
{ text-transform: none }

不幸的是,没有驼峰式文本转换。

您始终可以使用 Javascript 来适当地转换文本,或者如果您使用的是 PHP 或 ASP 等脚本语言,则可以在其中进行更改。

以下是取自 strtoupper PHP 文档页面的示例:

function strtocamel($str, $capitalizeFirst = true, $allowed = 'A-Za-z0-9') {
    return preg_replace(
        array(
            '/([A-Z][a-z])/e', // all occurances of caps followed by lowers
            '/([a-zA-Z])([a-zA-Z]*)/e', // all occurances of words w/ first char captured separately
            '/[^'.$allowed.']+/e', // all non allowed chars (non alpha numerics, by default)
            '/^([a-zA-Z])/e' // first alpha char
        ),
        array(
            '" ".$1', // add spaces
            'strtoupper("$1").strtolower("$2")', // capitalize first, lower the rest
            '', // delete undesired chars
            'strto'.($capitalizeFirst ? 'upper' : 'lower').'("$1")' // force first char to upper or lower
        ),
        $str
    );
}

There's a few things you can do with CSS for text transformations, here's all of them.

.Capitalize
{ text-transform: capitalize }

.Uppercase
{ text-transform: uppercase }

.Lowercase
{ text-transform: lowercase }

.Nothing
{ text-transform: none }

Unfortunately there is no Camel Case text-transform.

You could always use Javascript to transform the text appropriately or if you are using a scripting language such as PHP or ASP then change it in there.

Here's an example taken from the strtoupper PHP doc page:

function strtocamel($str, $capitalizeFirst = true, $allowed = 'A-Za-z0-9') {
    return preg_replace(
        array(
            '/([A-Z][a-z])/e', // all occurances of caps followed by lowers
            '/([a-zA-Z])([a-zA-Z]*)/e', // all occurances of words w/ first char captured separately
            '/[^'.$allowed.']+/e', // all non allowed chars (non alpha numerics, by default)
            '/^([a-zA-Z])/e' // first alpha char
        ),
        array(
            '" ".$1', // add spaces
            'strtoupper("$1").strtolower("$2")', // capitalize first, lower the rest
            '', // delete undesired chars
            'strto'.($capitalizeFirst ? 'upper' : 'lower').'("$1")' // force first char to upper or lower
        ),
        $str
    );
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文