如何使用 PHP 将字符串按大写字母分解?
我有一个字符串:CamelCaseString
我想对大写字母进行explode()、split() 或一些更好的方法来将该字符串分解为单个单词。
最简单的方法是什么?
--- 解决方案更新 ---
此链接指向一个略有不同的问题,但我认为答案通常比本页当前问题的答案更有用: 如何我在字符串中的大写字母处添加了一个空格,但使用 PHP 和正则表达式将连续的大写字母保持在一起?
I have a string: CamelCaseString
I want to explode(), split(), or some better method on capital letters to break this string down into the individual words.
What's the simplest way to do this?
--- SOLUTION UPDATE ---
This link is to a slightly different question, but I think the answer will generally be more useful than the answers to the current question on this page:
How can I add a space in string at capital letters, but keep continuous capitals together using PHP and a Regex?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您应该使用正则表达式。
试试这个:
preg_match_all('/[AZ][^AZ]*/', "CamelCaseString", $results);
包含所有单词的数组将保存在 $results[0] 中。
You should use regular expressions.
Try this:
preg_match_all('/[A-Z][^A-Z]*/', "CamelCaseString", $results);
The array containing all the words will be saved in $results[0].
这似乎也有效
,而且它不会分解较长的大写字母。即“MySQL”,将变成“My”和“SQL”
This seems to work too
And it won't break up longer runs of uppercase letters. I.e. "MySQL", will become "My" and "SQL"
split() 函数已被弃用,因此我将寻找使用其他函数的解决方案。
The split() function has been deprecated so I would look for a solution that uses some other function.