Return to Snippet

Revision: 29388
at December 14, 2010 11:06 by Sverri


Updated Code
if ( ! function_exists('camelCase'))
{
  function camelCase($subject, $delimiters=' _-', $lcfirst=true)
  {
    if ( ! is_string($subject))
    {
      throw new Exception("Subject must be of type string");
    }
    $subject = preg_replace('/[\s]+/', ' ', $subject);
    
    $subject = preg_split("/[$delimiters]/", $subject, -1, PREG_SPLIT_NO_EMPTY);
    
    foreach ($subject as $key => &$word)
    {
      $word = preg_replace('/[[:punct:]]/', '', $word);
      
      if (preg_match('/[A-Z]+$/', $word)) $word = ucfirst($word);
      
      else $word = ucfirst( strtolower($word) );
    }
    $subject = implode('', $subject);
    
    if ($lcfirst)
    {
      return function_exists('lcfirst') ? lcfirst($subject)
      :
      strtolower($subject[0]).substr($subject,1);
    }
    return $subject;
  }
}

// "usingPHPTurnThisIntoCamelCASENow"
echo camelCase("Using PHP turn This into   camelCASE, now!");

Revision: 29387
at December 14, 2010 11:04 by Sverri


Updated Code
if ( ! function_exists('camelCase'))
{
  function camelCase($subject, $delimiters=' _-', $lcfirst=true)
  {
    if ( ! is_string($subject))
    {
      throw new Exception("Subject must be of type string");
    }
    $subject = preg_replace('/[\s]+/', ' ', $subject);
    
    $subject = preg_split("/[$delimiters]/", $subject, -1, PREG_SPLIT_NO_EMPTY);
    
    foreach ($subject as $key => &$word)
    {
      $word = preg_replace('/[[:punct:]]/', '', $word);
      
      if (preg_match('/[A-Z]+$/', $word)) $word = ucfirst($word);
      
      else $word = ucfirst( strtolower($word) );
    }
    $subject = implode('', $subject);
    
    if ($lcfirst)
    {
      return function_exists('lcfirst') ? lcfirst($subject)
      :
      strtolower($subject[0]).substr($subject,1);
    }
    return $subject;
  }
}

// "usingPHPTurnThisIntoCamelCASE"
echo camelCase("Using PHP turn This into   camelCASE, now!");

Revision: 29386
at August 9, 2010 20:50 by Sverri


Updated Code
if ( ! function_exists('camelCase'))
{
  function camelCase($subject, $delimiters = ' _-')
  {
    if ( ! is_string($subject))
    {
      return '';
    }
    $subject = preg_replace('/[\s]+/', ' ', $subject);
    
    $subject = preg_split("/[$delimiters]/", $subject);
    
    foreach ($subject as &$word)
    {
      $word = preg_replace('/[[:punct:]]/', '', $word);
      
      if (preg_match('/^[A-Z]{0,5}$/', $word))
      {
        continue;
      }
      $word = ucfirst( strtolower($word) );
    }
    $subject = implode('', $subject);
    
    if (function_exists('lcfirst'))
    {
      $subject = lcfirst($subject);
    }
    else
    {
      $subject = strtolower($subject[0]) . substr($subject, 1);
    }
    return $subject;
  }
}

// "usingPHPTurnThisIntoCamelCase"
echo camelCase("Using PHP turn this into   camel-case");

Revision: 29385
at August 9, 2010 20:49 by Sverri


Updated Code
if ( ! function_exists('camelCase'))
{
  function camelCase($subject, $delimiters = ' _-')
  {
    if ( ! is_string($subject))
    {
      return '';
    }
    $subject = preg_replace('/[\s]+/', ' ', $subject);
    
    $subject = preg_split("/[$delimiters]/", $subject);
    
    foreach ($subject as &$word)
    {
      $word = preg_replace('/[[:punct:]]/', '', $word);
      
      if (preg_match('/^[A-Z]{0,5}$/', $word))
      {
        continue;
      }
      $word = ucfirst( strtolower($word) );
    }
    $subject = implode('', $subject);
    
    if (function_exists('lcfirst'))
    {
      $subject = lcfirst($subject);
    }
    else
    {
      $subject = strtolower($subject[0]) . substr($subject, 1);
    }
    return $subject;
  }
}

Revision: 29384
at July 28, 2010 07:49 by Sverri


Updated Code
function camelCase($subject, $delimiter=' ', $firstLower=true)
{
  // I needz a string, stooopid.
  if ( ! is_string($subject))
  {
    trigger_error("camelCase() only accepts strings");
    
    return 0;
  }
  // Loop over words in the string.
  foreach (explode($delimiter, $subject) as $val)
  {
    // Remove punctuation marks, if any.
    $val = preg_replace('/[[:punct:]]/', '', $val);
    
    // Do not touch acronyms.
    if (preg_match('/^[A-Z]+$/', $val))
    {
      $str .= $val; continue;
    }
    // If first word's first letter should be lowercase.
    if ($firstLower && !$firstLower=false)
    {
      $str .= lcfirst($val); continue;
    }
    // First letter uppercase.
    $str .= ucfirst($val);
  }
  // Kaboom!
  return $str;
}

// "thisIsTurnedIntoCamelcase"
echo camelCase("This is turned into camelcase");

Revision: 29383
at July 28, 2010 07:37 by Sverri


Initial Code
function camelCase($subject, $delimiter=' ', $firstLower=true)
{
  // I needz a string, stooopid.
  if ( ! is_string($subject))
  {
    trigger_error("camelCase() only accepts strings");
  }
  // Loop over words in the string.
  foreach (explode($delimiter, $subject) as $val)
  {
    // Remove punctuation marks, if any.
    $val = preg_replace('/[[:punct:]]/', '', $val);
    
    // Do not touch acronyms.
    if (preg_match('/^[A-Z]+$/', $val))
    {
      $str .= $val; continue;
    }
    // If first word's first letter should be lowercase.
    if ($firstLower && !$firstLower=false)
    {
      $str .= lcfirst($val); continue;
    }
    // First letter uppercase.
    $str .= ucfirst($val);
  }
  // Kaboom!
  return $str;
}

// "thisIsTurnedIntoCamelcase"
echo camelCase("This is turned into camelcase");

Initial URL


Initial Description
Turn a string into camelcase. You can optionally set a custom regex delimiter.

Initial Title
Camel Case a string

Initial Tags


Initial Language
PHP