Search
 
SCRIPT & CODE EXAMPLE
 

PHP

php function to convert string to camelcase

echo ucwords("hello world");
Comment

php camelcase to snake case

function from_camel_case($input) {
  $pattern = '!([A-Z][A-Z0-9]*(?=$|[A-Z][a-z0-9])|[A-Za-z][a-z0-9]+)!';
  preg_match_all($pattern, $input, $matches);
  $ret = $matches[0];
  foreach ($ret as &$match) {
    $match = $match == strtoupper($match) ?
      	strtolower($match) :
    	lcfirst($match);
  }
  return implode('_', $ret);
}

// Tests:
foreach ([
  'simpleTest' => 'simple_test',
  'easy' => 'easy',
  'HTML' => 'html',
  'simpleXML' => 'simple_xml',
  'PDFLoad' => 'pdf_load',
  'startMIDDLELast' => 'start_middle_last',
  'AString' => 'a_string',
  'Some4Numbers234' => 'some4_numbers234',
  'TEST123String' => 'test123_string',
] as $test => $result) {
  $output = from_camel_case($test);
  if ($output === $result) {
    echo "Pass: $test => $result
";
  } else {
    echo "Fail: $test => $result [$output]
";
  }
}
/*
Pass: simpleTest => simple_test
Pass: easy => easy
Pass: HTML => html
Pass: simpleXML => simple_xml
Pass: PDFLoad => pdf_load
Pass: startMIDDLELast => start_middle_last
Pass: AString => a_string
Pass: Some4Numbers234 => some4_numbers234
Pass: TEST123String => test123_string
*/
Comment

PREVIOUS NEXT
Code Example
Php :: concat php 
Php :: filter child table data from parent laravel eloquent 
Php :: get key of array element php 
Php :: get file extension php 
Php :: php random 
Php :: json encode decode php 
Php :: wordpress single post template 
Php :: how to make a config file for php 
Php :: php remove first word from string 
Php :: string match percentage php 
Php :: laravel auth check login 
Php :: uninstall phpstorm ubuntu 
Php :: wordpress deactivate widgets gutenberg 
Php :: wordpress move debug.log 
Php :: symfony no php binaries detected 
Php :: Adding JavaScript to a Specific WordPress Page Using Code In Header 
Php :: laravel value eloquent 
Php :: php get last digit of number 
Php :: CSV File Read using PHP fgetcsv() 
Php :: wp php go back 
Php :: laravel factory relationship one to many 
Php :: laravel casts 
Php :: include() in php 
Php :: sort array php by key 
Php :: laravel run all seeders 
Php :: null value in php 
Php :: laravel database seeder 
Php :: slugify text in php 
Php :: calculator in php 
Php :: create array php 
ADD CONTENT
Topic
Content
Source link
Name
7+3 =