The essence of the task is to convert strings from the Var_text_text format to the varTextText format.

Var_text_text => varTextText

  • Everything is working . thanks friend - RoyFace

3 answers 3

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); 
  • $ new_str = lcfirst ($ new_str); - RoyFace
  • No need to use explode in a loop. - Take out the variable. - And

You 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 
  • Well, a step forward to think? A solution with a fixed number of parts is useless. - vp_arth
  • @vp_arth thought - fixed, ATP) - DROP

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;