0

Trying to convert something like WebsiteURL to website_url. I was able to convert it to website_u_r_l using this suggestion: PHP - add underscores before capital letters

However, that doesn't work for my requirements. Having a hard time finding a better answer. Maybe the simpler solution is to convert multiple capitals in a row first, so WebsiteUrl? Wondering what the most efficient route would be.

Bhargav Rao
  • 50,140
  • 28
  • 121
  • 140
roubix
  • 11
  • 2

2 Answers2

1
 echo strtolower(preg_replace('%([a-z])([A-Z])%', '\1_\2', "WebsiteURL")); 

check solution here

Azouz Mohamadi
  • 141
  • 1
  • 2
0

This code below achieves what I think you want to achieve

$result = strtolower(preg_replace('/(.)([A-Z])/', '$1_$2', $subject));

Converting

TestTest
testTest
testingTest

To:

testing_test
test_test
test_test

Found at this src

Isaac
  • 784
  • 10
  • 23