The variable name uses a hump, and the field in the database contains underscores. Now I want to convert userId and other variables into user_id in batches. How can I get the position of uppercase letters in the string? The variable name uses a hump, and the field in the database contains underscores. Now I want to convert userId and other variables into user_id in batches. How can I get the position of uppercase letters in the string?
Reply content:
The variable name uses a hump, and the field in the database contains underscores. Now I want to convert userId and other variables into user_id in batches. How can I get the position of uppercase letters in the string?
Http://3v4l.org/kC52c
echo preg_replace_callback("/[A-Z]/", function($ma){return "_".strtolower($ma[0]);}, "userId"); //user_id
Thank you @ brief for your answer. I found another practical website.
The answer to explainOpenZIPChangeopen_z_i_p.
The following is my code:
Http://3v4l.org/hIQUW
echo strtolower(preg_replace('/((?<=[a-z])(?=[A-Z]))/', '_', 'AbcDefGhijk'));
Try it.OpenZIPChangedopen_zip.
From:Laravel4
/** * Convert a string to snake case. * * @param string $value * @param string $delimiter * @return string */ function snake($value, $delimiter = '_') { $replace = '$1'.$delimiter.'$2'; return ctype_lower($value) ? $value : strtolower(preg_replace('/(.)([A-Z])/', $replace, $value)); }
Echo preg_replace ('/([A-Z])/', '_ $ 1', 'abcdefghijk'); it seems easier