There is an alternate solution. I mean, it saves not only digits together, and abbreviations too (in PHP):
preg_replace(
'/(?<!^)([A-Z][a-z]|(?<=[a-z])[^a-z]|(?<=[A-Z])[0-9_])/',
'_$1',
$str
)
This regex will work for these cases:
'fat' ---> 'fat'
'fatBat' ---> 'fat_bat'
'FatBat' ---> 'fat_bat'
'camera360' ---> 'camera_360'
'camera360all' ---> 'camera_360all'
'camera360All' ---> 'camera_360_all'
'cameraABC' ---> 'camera_abc'
'cameraABCAll' ---> 'camera_abc_all'
'thirdLineOfText87' ---> 'third_line_of_text_87'
This solution to lower case. But if we want upper case, we may use \U
-modifier as in above solution in notepad++:
- Find What:
/(?<!^)([A-Z][a-z]|(?<=[a-z])[^a-z]|(?<=[A-Z])[0-9_])/
- Replace With:
_\1
- Match Case: ON.
I found this solution on doc page of php function preg_replace: https://www.php.net/manual/en/function.preg-replace.php#111695 .