douluoxiao2286 2015-01-05 17:51
浏览 39
已采纳

正则表达式将包含字符串的缩写转换为标题案例

As part of a simple URL cleaning/slugging function I'm extending, I need to make conversions such as the following:

Original               Converted
---------------------------------------------
USAMarch               UsaMarch
FETExaminations        FetExaminations      *
AnotherABBRString      AnotherAbbrString
LastONE                LastOne

(These are just examples, except for the second, which is what got me to doing this in the first place.)

I'm assuming I need to use preg_replace_callback to determine the position of any abbreviations and case them appropriately.

I haven't the foggiest of where to begin. Does anyone know what I can do to get this right?

Update

Okay, I have this so far:

$input = preg_replace_callback("~([A-Z])([A-Z]+)([^a-z])([a-z]|)~",
function ($captures) {
   return $captures[1].strtolower($captures[2]).$captures[3].$captures[4];
},
$input);

Unfortunately, however, it doesn't work with strings where the abbreviation is last:

This                   Becomes
---------------------------------------------
LastONE                LastOnE

So I'm checking the end incorrectly, I assume. Also, this attempt isn't recursive. How would I do that?

  • 写回答

1条回答 默认 最新

  • 普通网友 2015-01-05 18:45
    关注

    These work with your examples.

    Even finds stand alone caps.

     # Find:  '/([A-Z])([A-Z]+)(?=[A-Z]|\b)/'
     # Replace:  $1 . tolower($2)
    
     ( [A-Z] )                     # (1), Upper case
     ( [A-Z]+ )                    # (2), 1 or more upper case
     (?=                           # Lookahead assertion
          [A-Z]                         # Upper case
       |                              # or,
          \b                            # Word boundry
     )
    

    Or, requires a lower case before possible end caps

     # Find:  '/([a-z])?([A-Z])([A-Z]+)(?=[A-Z]|(?(1)\b|(?!)))/'
     # Replace:  $1$2 . tolower($3)
    
     ( [a-z] )?                    # (1), optional lower case
     ( [A-Z] )                     # (2), Upper case
     ( [A-Z]+ )                    # (3), 1 or more upper case
     (?=                           # Lookahead assertion
          [A-Z]                         # Upper case
       |                              # or
          (?(1)                         # Conditional, does lower case precede this ?
               \b                            # yes, match boundry
            |  (?!)                          # or fail, this is a stand alone cap's
          )
     )
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了