The essence of the task is to convert strings from the Var_text_text format to the varTextText format.
Var_text_text => varTextText
The essence of the task is to convert strings from the Var_text_text format to the varTextText format.
Var_text_text => varTextText
Hold buddy, and do not forget to read off the answer ;-)
$old_str = 'Var_text_text'; foreach (explode('_', $old_str) as $key => $str) { if ($key == 0) { $new_str = strtolower($str); } else { $new_str .= ucfirst(strtolower($str)); } } print_r($new_str); explode in a loop. - Take out the variable. - AndYou can still in this way (more concise):
$str = 'Var_text1_text2_text3'; echo join(array_map(function($str){ static $i = 0; return $i++ < 1 ? lcfirst($str) : ucfirst($str); }, explode('_', $str))); The result of the script:
varText1Text2Text3 Method one
$str = 'Var_text_text'; // ΠΡΠ΅ΠΎΠ±ΡΠ°Π·ΡΠ΅ΠΌ ΠΊΠ°ΠΆΠ΄ΡΡ ΠΏΠ΅ΡΠ²ΡΡ Π±ΡΠΊΠ²Ρ ΡΠ»ΠΎΠ²Π° Π² Π²Π΅ΡΡ
Π½ΠΈΠΉ ΡΠ΅Π³ΠΈΡΡΡ // Π‘Π»ΠΎΠ²Π° ΡΠ°Π·Π΄Π΅Π»ΡΡΡΡΡ ΡΠΈΠΌΠ²ΠΎΠ»ΠΎΠΌ '_' $res = ucwords($str, '_'); // Π£Π΄Π°Π»ΡΠ΅ΠΌ ΡΠΈΠΌΠ²ΠΎΠ» '_' ΠΈΠ· ΡΡΡΠΎΠΊΠΈ $res = str_replace('_', '', $res); // ΠΠ΅ΡΠ΅Π²ΠΎΠ΄ΠΈΠΌ ΠΏΠ΅ΡΠ²ΡΡ Π±ΡΠΊΠ²Ρ Π² Π½ΠΈΠΆΠ½ΠΈΠΉ ΡΠ΅Π³ΠΈΡΡΡ $res = lcfirst($res); echo $res; Second way
$str = 'Var_text_text'; // ΡΠ°Π·Π΄Π΅Π»ΡΠ΅ΠΌ ΡΡΡΠΎΠΊΡ Π½Π° ΡΠ°ΡΡΠΈ ΠΏΠΎ ΡΠΈΠΌΠ²ΠΎΠ»Ρ '_' // ΠΈ Π·Π°ΠΏΠΈΡΡΠ²Π°Π΅ΠΌ ΡΡΠΈ ΡΠ°ΡΡΠΈ ΡΡΡΠΎΠΊΠΈ Π² ΠΌΠ°ΡΡΠΈΠ² $parts = explode('_', $str); $res = ''; // ΡΠΈΠΊΠ» ΠΏΠΎ Π²ΡΠ΅ΠΌ ΡΠ°ΡΡΡΠΌ foreach($parts as $part) { // ΡΠΊΠ»Π΅ΠΈΠ²Π°Π΅ΠΌ ΡΡΡΠΎΠΊΡ ΠΈΠ· ΡΠ°ΡΡΠ΅ΠΉ, // ΠΏΡΠΈ ΡΡΠΎΠΌ ΠΏΠ΅ΡΠ²ΡΡ Π±ΡΠΊΠ²Ρ ΠΊΠ°ΠΆΠ΄ΠΎΠΉ ΡΠ°ΡΡΠΈ ΠΏΠ΅ΡΠ΅Π²ΠΎΠ΄ΠΈΠΌ Π² Π½ΠΈΡ
Π½ΠΈΠΉ ΡΠ΅Π³ΠΈΡΡΡ $res .= ucfirst($part); } // ΠΠ΅ΡΠ΅Π²ΠΎΠ΄ΠΈΠΌ ΠΏΠ΅ΡΠ²ΡΡ Π±ΡΠΊΠ²Ρ Π² Π½ΠΈΠΆΠ½ΠΈΠΉ ΡΠ΅Π³ΠΈΡΡΡ $res = lcfirst($res); echo $res; Source: https://ru.stackoverflow.com/questions/820182/
All Articles