Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3240988
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T18:08:51+00:00 2026-05-17T18:08:51+00:00

What is quicker, for camelCase to underscores, preg_replace() or ord() ? My guess is

  • 0

What is quicker, for camelCase to underscores, preg_replace() or ord()?

My guess is that using ord() will be quicker, since preg_replace() can do much more than needed.

function __autoload($class_name) {
  $name = strtolower(preg_replace('/([a-z])([A-Z])/', '$1_$2', $class_name));
  require_once "some_dir/" . $name . ".php";
}
function __autoload($class_name) {
  $class_name[0] = strtolower($class_name[0]);
    
  $len = strlen($class_name);
  for ($i = 0; $i < $len; ++$i) {
    if (ord($class_name[$i]) > ord('A') && ord($class_name[$i]) < ord('Z')) {
      $class_name[$i] = '_' . strtolower($class_name[$i]);
      ++$len;
      ++$i;
    }
  }
    
  return $class_name;
}

Disclaimer

Code examples are taken from Convert CamelCase to under_score_case in php __autoload().

Edit

After jensgram‘s array-suggestion and finding array_splice(), I have come up with the following.

function __autoload ($string) {
  $string = str_split($string);
  $pos = count($string);

  while (--$pos > 0) {
    $lower = strtolower($string[$pos]);

    if ($string[$pos] === $lower) {
      continue;
    }
    
    unset($string[$pos]);
    array_splice($string, $pos, 0, array('_', $lower));
  }

  $string  =  implode('', $string);
  return $string;
}

When I will test these methods, I will add this one, but feel free to tell me your results anytime.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T18:08:51+00:00Added an answer on May 17, 2026 at 6:08 pm

    i think (and i’m pretty much sure) that the preg_replace method will be faster – but if you want to know, why dont you do a little benchmark calling both functions 100000 times and measure the time?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there a quicker, more efficient means of doing so than using a ListIterator
Is there a more efficient/quicker/more sensible to copy part of an ArrayList than the
Why is COUNT(*) so much quicker than COUNT(field) , Even when field is an
Would having a nice little feature that makes it quicker to write code like
In PHP, which is quicker; using include('somefile.php') or querying a MySQL database with a
Using Umbraco, which is quicker to load on a page... XSLT Macro or a
Is there a quicker way than the following to 'flip' a true or false
Which language will allow a beginner to get up to speed quicker? Basically which
I have the following sql statement that I need to make quicker. There are
Dreadful title right? Thought I'd see if Stack overflow is quicker than me testing

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.