PHP - add underscores before capital letters

前端 未结 5 2276
梦如初夏
梦如初夏 2021-02-18 14:48

How can I replace a set of words that look like:

SomeText

to

Some_Text

?

5条回答
  •  -上瘾入骨i
    2021-02-18 15:36

    The simplest way to do this is with a regular expression replacement.

    For example:

    substr(preg_replace('/([A-Z])/', '_$1', 'SomeText'),1);
    

    The substr call there is to remove a leading '_'

提交回复
热议问题