如何将驼峰式大小写解析为人类可读的字符串?

发布于 2024-11-14 15:16:30 字数 709 浏览 4 评论 0原文

是否可以将驼峰式字符串解析为更具可读性的内容。

例如:

  • 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 技术交流群。

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

发布评论

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

评论(2

我纯我任性 2024-11-21 15:16:30

PHP手册中str_split的用户注释中有一些例子。

来自凯文

<?php
$test = 'CustomerIDWithSomeOtherJETWords';

preg_replace('/(?!^)[A-Z]{2,}(?=[A-Z][a-z])|[A-Z][a-z]/', ' $0', $test);

这是我为满足您的帖子要求而写的内容:

<?php
$tests = array(
    'LocalBusiness' => 'Local Business',
    'CivicStructureBuilding' => 'Civic Structure Building',
    'getUserMobilePhoneNumber' => 'Get User Mobile Phone Number',
    'bandGuitar1' => 'Band Guitar 1',
    'band2Guitar123' => 'Band 2 Guitar 123',
);

foreach ($tests AS $input => $expected) {
    $output = preg_replace(array('/(?<=[^A-Z])([A-Z])/', '/(?<=[^0-9])([0-9])/'), ' $0', $input);
    $output = ucwords($output);
    echo $output .' : '. ($output == $expected ? 'PASSED' : 'FAILED') .'<br>';
}

There are some examples in the user comments of str_split in the PHP manual.

From Kevin:

<?php
$test = 'CustomerIDWithSomeOtherJETWords';

preg_replace('/(?!^)[A-Z]{2,}(?=[A-Z][a-z])|[A-Z][a-z]/', ' $0', $test);

And here's something I wrote to meet your post's requirements:

<?php
$tests = array(
    'LocalBusiness' => 'Local Business',
    'CivicStructureBuilding' => 'Civic Structure Building',
    'getUserMobilePhoneNumber' => 'Get User Mobile Phone Number',
    'bandGuitar1' => 'Band Guitar 1',
    'band2Guitar123' => 'Band 2 Guitar 123',
);

foreach ($tests AS $input => $expected) {
    $output = preg_replace(array('/(?<=[^A-Z])([A-Z])/', '/(?<=[^0-9])([0-9])/'), ' $0', $input);
    $output = ucwords($output);
    echo $output .' : '. ($output == $expected ? 'PASSED' : 'FAILED') .'<br>';
}
十秒萌定你 2024-11-21 15:16:30

这是我尝试将 PascalCase 和 CamelCase 字符串解析为空格分隔的标题大小写的看法。模式内注释应该有助于描述每个子模式正在做什么。

代码:(演示

$tests = [
    'LocalBusiness' => 'Local Business',
    'CivicStructureBuilding' => 'Civic Structure Building',
    'getUserMobilePhoneNumber' => 'Get User Mobile Phone Number',
    'bandGuitar1' => 'Band Guitar 1',
    'band2Guitar123' => 'Band 2 Guitar 123',
    'CustomerIDWithSomeOtherJETWords' => 'Customer ID With Some Other JET Words',
    'noOneIsMightierThanI' => 'No One Is Mightier Than I',
    'USAIsNumber14' => 'USA Is Number 14',
    '99LuftBallons' => '99 Luft Ballons',
];

$result = [];
foreach ($tests as $input => $expected) {
    $newString = ucwords(
        preg_replace(
            '/(?:
               [A-Z]+?         (?=\d|[A-Z][a-z])  #acronyms
               |[A-Z]?[a-z]+   (?=[^a-z])         #words
               |\d+            (?=\D)             #numbers
               |(*SKIP)(*FAIL)                    #abort
              )\K
             /x',
            ' ',
            $input
        )
    );
    $result[] = ($newString === $expected ? 'PASSED' : 'FAILED') . ': "' . $newString . '"';
}
var_export($result);
  • 通过匹配 1 个或多个大写字母后跟来找到首字母缩略词一个数字或一个单词。
  • 单词是通过匹配小写字母来找到的,这些字母前面可能有也可能没有大写字母。
  • 数字是通过匹配 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)

$tests = [
    'LocalBusiness' => 'Local Business',
    'CivicStructureBuilding' => 'Civic Structure Building',
    'getUserMobilePhoneNumber' => 'Get User Mobile Phone Number',
    'bandGuitar1' => 'Band Guitar 1',
    'band2Guitar123' => 'Band 2 Guitar 123',
    'CustomerIDWithSomeOtherJETWords' => 'Customer ID With Some Other JET Words',
    'noOneIsMightierThanI' => 'No One Is Mightier Than I',
    'USAIsNumber14' => 'USA Is Number 14',
    '99LuftBallons' => '99 Luft Ballons',
];

$result = [];
foreach ($tests as $input => $expected) {
    $newString = ucwords(
        preg_replace(
            '/(?:
               [A-Z]+?         (?=\d|[A-Z][a-z])  #acronyms
               |[A-Z]?[a-z]+   (?=[^a-z])         #words
               |\d+            (?=\D)             #numbers
               |(*SKIP)(*FAIL)                    #abort
              )\K
             /x',
            ' ',
            $input
        )
    );
    $result[] = ($newString === $expected ? 'PASSED' : 'FAILED') . ': "' . $newString . '"';
}
var_export($result);
  • 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.
  • anything that does not match should be prohibited from qualifying for space injection.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文