PHP - add underscores before capital letters

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

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

SomeText

to

Some_Text

?

5条回答
  •  执念已碎
    2021-02-18 15:25

    $result = strtolower(preg_replace('/(.)([A-Z])/', '$1_$2', $subject));

    Converts:

    HelloKittyOlolo
    Declaration
    CrabCoreForefer
    TestTest
    testTest
    

    To:

    hello_kitty_ololo
    declaration
    crab_core_forefer
    test_test
    test_test
    

提交回复
热议问题