How to convert camelCase to underscore in php?
Use PHP to convert the camelCase string into an underscore-separated or other character separated.
1. Code:
<?php
function convertCamelCaseToUnderscore($str, $separator = "_"){
$res = '';
for ($i = 0; $i < strlen($str); $i++) {
$ch = $str[$i];
if (ctype_upper($ch)) {
$res .= $separator.strtolower($ch);
} else {
$res .= $ch;
}
}
return trim($res);
}
$str = "soltutsComIsProvidingPhpTutorials";
echo convertCamelCaseToUnderscore($str);
echo "<br>";
echo convertCamelCaseToUnderscore($str, " ");
?>
2. Example:
Input:
soltutsComIsProvidingPhpTutorials
Output:
Soltuts_com_is_providing_php_tutorials