驼峰到破折号-两个大写字母彼此相邻 [英] camelCase to dash - two capitals next to each other

查看:217
本文介绍了驼峰到破折号-两个大写字母彼此相邻的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用此函数将CamelCase转换为虚线字符串:

I'm using this function to convert CamelCase to dashed string:

function camel2dashed($className) {
    return strtolower(preg_replace('/([^A-Z-])([A-Z])/', '$1-$2', $className));
}

这有点奏效,但是当我有前任时会有问题.此字符串:getADog.它返回get-adog,但我想要get-a-dog

it kinda works but theres problem when I have for ex. this string: getADog. It returns get-adog but I want get-a-dog

我应该如何更改我的代码?谢谢

how should I change my code? Thanks

推荐答案

使用前瞻断言:

function camel2dashed($className) {
    return strtolower(preg_replace('/([a-zA-Z])(?=[A-Z])/', '$1-', $className));
}

查看其在线运行情况: ideone

See it working online: ideone

这篇关于驼峰到破折号-两个大写字母彼此相邻的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆