CamelCase conversion function

4

I already have a function where it performs the conversion of strings to slug .

Below I will give examples for facilitation in understanding my question.

3 examples before conversion:

  • link para uma página
  • link página
  • link
  • 3 examples after conversion:

  • link-para-uma-pagina
  • link-pagina
  • link
  • So far so good, I've always used this function without major problems. But now I've found the need to use the CamelCase format starting from the slug that I have.

    I know there are plenty of possibilities to do this, so I'm here to get the best response possible.

      

    One detail is that it would be a "almost" CamelCase, because regardless of the string, I need the first character to always be in lowercase.

    3 examples after the slug conversion to the "almost" CamelCase:

  • linkParaUmaPagina
  • linkPagina
  • link
  • asked by anonymous 08.11.2015 / 19:37

    4 answers

    2

    Here's another option:

        function toCamelCase($string) {
           //Converte todas as '-' em espaço em branco para a função ucwords funcionar.
           $string = str_replace('-', ' ', $string);
    
           return str_replace(' ', '', lcfirst(ucwords($string)));
    } 
    
        
    09.11.2015 / 02:43
    2

    You can do it this way:

    <?php
    
    function toCamelCase($str) {
    
      $newStr = '';
    
      //$str = preg_replace('/['^~\'"]/', null, iconv('UTF-8', 'ASCII//TRANSLIT', $str));
    
      $pieces = explode('-', $str);
    
      for ($i=0; $i < count($pieces); $i++) {
    
        if ($i > 0) {
          $newStr .= ucfirst($pieces[$i]);
        } else {
          $newStr .= $pieces[$i];
        }
    
      }
    
      return $newStr;
    
    }
    
        
    08.11.2015 / 19:54
    2

    Example using regular expression:

    function StrToCamelCase($m){
        return $m[1].strtoupper($m[2]);
    }
    
    $prepended_char = '-';
    
    $str = 'link-para-uma-pagina';
    echo lcfirst(str_replace($prepended_char, '', (preg_replace_callback('/(^|[ \-'.$prepended_char.'])([a-z])/','StrToCamelCase',$str))));
    

    Although solving, I particularly prefer a simpler and more intuitive solution using explode() and loop repetition.

    The reason is that ER use is not always beneficial. It may seem more elegant with fewer codes, but that does not mean it's more performative.

    I have not tested performance in this particular case, but I believe that while , for or foreach with explode is better. If both ER or explode give a same execution time or an insignificant difference, I prefer the simple way where it is easier to understand and maintain.

    ERs are always complicated and obscure the code.

        
    08.11.2015 / 20:29
    1

    Another way to accomplish this task is to use a regex where default matches the dash followed by the next character that is expressed as: -(.) to do the override use strtoupper () in the group (that point in parentheses). An anonymous function was used

    <?php
    $str = 'link-para-uma-pagina';
    $str = preg_replace_callback('/-(.)/', function($item){return strtoupper($item[1]);}, $str);
    echo $str;
    

    Output:

    linkParaUmaPagina
    

    The first argument of preg_replace_callback is a regular expression for the replacement, the second is an anonymous function that leaves the capturing of the group in capsule so $item[1] and not $item[0] that is the capture of the entire expression.

    preg_replace_callback('/-(.)/', function($item){return strtoupper($item[1]);}, $str);
    
        
    09.11.2015 / 13:13