I need to separate a string with "_" every time it finds a capital letter and transform it to lowercase. This to separate each word from the class name, for example if I have the following class:
class UserNote{
}
the name of that class would look like: user_notes
I did this method but it only works when the class has 1 word:
public function parseClassName(){
$className = strtolower(get_class($this));
return $className . 's';
}
SOLVED:
Thanks guys, I've tried both answers and both work correctly. My method has stayed that way for each of the answers.
public function parseClassName(){
$className = preg_replace('/\s+/u', '', ucwords(get_class($this)));
$className = mb_strtolower(preg_replace('/(.)(?=[A-Z])/u', '$1_', $className));
return $className . 's';
}
The other method:
public function parseClassName(){
$className = preg_replace('/(?<=\w)(\p{Lu})/u', '_$1', get_class($this));
$className = mb_strtolower($className);
return $className . 's';
}
Thanks again.