如何将驼峰式大小写解析为人类可读的字符串?
是否可以将驼峰式字符串解析为更具可读性的内容。
例如:
- LocalBusiness = 本地企业
- CivicStructureBuilding = 市政结构建筑
- getUserMobilePhoneNumber = 获取用户手机号码
- bandGuitar1 = 乐队吉他 1
更新
使用 simshaun 正则表达式示例我设法分隔数字从具有此规则的文本:
function parseCamelCase($str)
{
return preg_replace('/(?!^)[A-Z]{2,}(?=[A-Z][a-z])|[A-Z][a-z]|[0-9]{1,}/', ' $0', $str);
}
//string(65) "customer ID With Some Other JET Words With Number 23rd Text After"
echo parseCamelCase('customerIDWithSomeOtherJETWordsWithNumber23rdTextAfter');
Is it possible to parse camel case string in to something more readable.
for example:
- LocalBusiness = Local Business
- CivicStructureBuilding = Civic Structure Building
- getUserMobilePhoneNumber = Get User Mobile Phone Number
- bandGuitar1 = Band Guitar 1
UPDATE
Using simshaun regex example I managed to separate numbers from text with this rule:
function parseCamelCase($str)
{
return preg_replace('/(?!^)[A-Z]{2,}(?=[A-Z][a-z])|[A-Z][a-z]|[0-9]{1,}/', ' $0', $str);
}
//string(65) "customer ID With Some Other JET Words With Number 23rd Text After"
echo parseCamelCase('customerIDWithSomeOtherJETWordsWithNumber23rdTextAfter');
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
PHP手册中str_split的用户注释中有一些例子。
来自凯文:
这是我为满足您的帖子要求而写的内容:
There are some examples in the user comments of str_split in the PHP manual.
From Kevin:
And here's something I wrote to meet your post's requirements:
这是我尝试将 PascalCase 和 CamelCase 字符串解析为空格分隔的标题大小写的看法。模式内注释应该有助于描述每个子模式正在做什么。
代码:(演示)
首字母缩略词
一个数字或一个单词。单词
是通过匹配小写字母来找到的,这些字母前面可能有也可能没有大写字母。数字
是通过匹配 1 个或多个数字后跟一个非数字来找到的。Here is my take on trying to parse PascalCase and camelCase strings into space-delimited titlecase. The in-pattern comments should help to describe what each subpattern is doing.
Code: (Demo)
acronyms
are found by matching 1 or more uppercase letters followed by either a number or a word.words
are found by matching lowercase letters which may or may not be preceded by an uppercase letter.numbers
are found by matching 1 or more digits which are followed by a non-digit.