如何删除字符串中的所有非大写字符?

编程入门 行业动态 更新时间:2024-10-10 11:22:28
本文介绍了如何删除字符串中的所有非大写字符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

是的,我基本上只是想把像 Social Inc.David Jason 之类的短语分解为 SIDJ.我试过使用explode但不知道如何爆炸所有BUT大写字母,我需要使用preg_match()吗?

Yeah I'm basically just trying to explode a phrase like Social Inc. or David Jason to SI and DJ. I've tried using explode but couldn't figure out how to explode everything BUT the capital letters, do I need to use preg_match()?

推荐答案

你可以使用这个正则表达式 (?![AZ]).preg_replace() 来替换除大写字符外的所有字符.

You can use this regex (?![A-Z]). with preg_replace() to replace every char except the one in uppercase.

preg_replace("/(?![A-Z])./", "", $yourvariable)

正则表达式将查找任何不是大写字母(?! 否定前瞻).
如果你想用其他情况测试它,我已经创建了一个 regex101.

The regex will look for anythings NOT an uppercase letter ( ?! negative lookahead ).
I've created a regex101 if you wish to test it with other cases.

EDIT 作为此线程的更新,您还可以使用方括号内的 ^ 字符来反转效果.

EDIT As an update of this thread, You could also use the ^ char inside the square braquets to reverse the effect.

preg_replace("/([^A-Z])./", "", $yourvariable)

这将匹配所有不是大写的字符并用空替换它们.

This will match all char that are not uppercase and replace them with nothing.

这篇关于如何删除字符串中的所有非大写字符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

更多推荐

[db:关键词]

本文发布于:2023-04-28 15:05:20,感谢您对本站的认可!
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字符串   字符

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!