dream198731 2017-11-22 01:20
浏览 57
已采纳

在跳过缩写时,将camel case格式化为可在PHP中读取

So i am stuck - I have looked at tons of answers in here, but none seems to resolve my last problem.

Through an API with JSON, I receive an equipment list in a camelcase format. I can not change that.

I need this camelcase to be translated into normal language -

So far i have gotten most words seperated through:

$string = "SomeEquipmentHere";

$spaced = preg_replace('/([A-Z])/', ' $1', $string);
var_dump($spaced);

string ' Some Equipment Here' (length=20)

$trimmed = trim($spaced);
var_dump($trimmed);
string 'Some Equipment Here' (length=19)

Which is working fine - But in some of the equipments consists of abbreviations

"ABSBrakes" - this would require ABS and separated from Brakes

I can't check for several uppercases next to each other since it will then keep ABS and Brakes together - there are more like these, ie: "CDRadio"

So what is want is the output to be:

"ABS Brakes"

Is there a way to format it so, if there is uppercases next to eachother, then only add a space before the last uppercase letter of that sequence?

I am not strong in regex.

EDIT

Both contributions are awesome - people coming here later should read both answers

The last problems to consists are the following patterns :

"ServiceOK" becomes "Service O K"

"ESP" becomes "ES P"

The pattern only consisting of a pure uppercased abbreviation is fixed by a function counting lowercase letter, if there is none, it will skip over the preg_replace().

But as Flying wrote in the comments on his answer, there could potentially be a lot of instances not covered by his regex, and an answer could be impossible - I don't know if this could be a challenge for the regex.

Possibly by adding some "If there is not a lowercase after the uppercase, there should not be inserted a space" rule

展开全部

  • 写回答

2条回答 默认 最新

  • dongshan4878 2017-11-22 04:28
    关注

    Here is a single-call pattern that doesn't use any anchors, capture groups, or references in the replacement string: /(?:[a-z]|[A-Z]+)\K(?=[A-Z]|\d+)/

    Pattern&Replace Demo

    Code: (Demo)

    $tests = [
        'SomeEquipmentHere',
        'ABSBrakes',
        'CDRadio',
        'Valve14',
    ];
    foreach ($tests as $test) {
        echo preg_replace('/(?:[a-z]|[A-Z]+)\K(?=[A-Z]|\d+)/',' ',$test),"
    ";
    }
    

    Output:

    Some Equipment Here
    ABS Brakes
    CD Radio
    Valve 14
    

    This is a better method because there is nothing to mop up. If there are new strings to consider (that break my method), please leave them in a comment so that I can update my pattern.

    Pattern Explanation:

    /         #start the pattern
    (?:[a-z]  #match 1 lowercase letter
    |         #or
    [A-Z]+)   #1 or more uppercase letters
    \K        #restart the fullstring match (forget the past)
    (?=[A-Z]  #look-ahead for 1 uppercase letter
    |         #or
    \d+)      #1 or more digits
    /         #end the pattern
    

    Edit:

    There are some other patterns that may provide better accuracy including:

    /(?:[a-z]|\B[A-Z]+)\K(?=[A-Z]\B|\d+)/
    

    Granted, the above pattern will not properly handle ServiceOK

    Demo Link Word Boundaries Link


    or this pattern with an anchor:

    /(?!^)(?=[A-Z][a-z]+|(?<=\D)\d)/
    

    The above pattern will accurately split: SomeEquipmentHere, ABSBrakes, CDRadio, Valve14, ServiceOK, ESP as requested by the OP.

    Demo Link

    *Note: Pattern accuracy can be improved as more sample strings are provided.

    展开全部

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)
编辑
预览

报告相同问题?

悬赏问题

  • ¥15 已知手指抓握过程中掌指关节、手指各关节和指尖每一帧的坐标,用贝塞尔曲线可以拟合手指抓握的运动轨迹吗?
  • ¥50 libwebsockets 如何添加其他socket事件回调
  • ¥50 实现画布拖拽算子排布,通过flink实现算子编排计算,请提供思路
  • ¥15 esium自定义材质拉伸问题
  • ¥15 cmake+mingw使用<mysqlx/xdevapi.h>报错
  • ¥15 eNSP中防火墙的使用
  • ¥15 不能对数据库增删改但是可以查询
  • ¥15 关于#mlnet#的问题:mlnet相关请求(语言-c#)
  • ¥15 lvgl7.11怎么做出文字被选中的效果
  • ¥50 如何快速查看手机目标app的主要服务器ip
手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部