Camelcase conversion function

Asked

Viewed 1,466 times

5

I already have a function where it performs the conversion of strings for Slug.

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

3 examples before conversion:

  1. link para uma página
  2. link página
  3. link

3 examples after conversion:

  1. link-para-uma-pagina
  2. link-pagina
  3. link

So far so good, always used this function without major problems. Only now I found the need to use the format Camelcase starting from the Slug that I have.

I know there are countless possibilities of that being done, so I’m here to get the best possible answer.

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

3 examples after converting from Slug to "almost" Camelcase:

  1. linkParaUmaPagina
  2. linkPagina
  3. link

4 answers

3


Here’s one more 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)));
} 

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;

}
  • Very good Yure, that’s right, just remembering that in this case would have to 'explode' in '-' and not in ' ', because the format of the string will already be in Slug format as I said in the question link-para-uma-pagina. And in this case it would not be necessary also removal of accents and similar.

  • 1

    So I’ve already made the change in function.

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 solve, particularly prefer a simpler and more intuitive solution using explode() and loop loop loop.

The reason is that not always the use of ER is advantageous. It may seem more elegant with fewer codes, but that doesn’t mean it’s more performative.

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

Ers are always complicated and obscure the code.

  • 1

    It is agree with you with the simple and intuitive, but I asked this question precisely for this, to have other options beyond the simple and intuitive, depending on the frequency of use maybe it is not so cool to "live" making loop to perform the function every time, I don’t know.

  • 1

    added the reason in the reply

1

Another way to perform this task is to use a regex where default matches the dash followed by the next character which is expressed as: -(.) to make the subustituition use strtoupper() group(that point between 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;

Exit:

linkParaUmaPagina

The first argument of preg_replace_callback is regular expression for substitution, the second is an anonymous function that leaves the capture of the group in upper case so $item[1] and not $item[0] which is the capture of all expression.

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.