I have a string that looks like this:
$string = 'helloWorldAndStackOverflow';
I want it to end with:
$string = 'hello.world.and.stack.overflow';
So where I have a uppercase character it should prepend a .
character. Then I can use strtolower
on the whole thing.
How can I prepend the dot?
You can use preg_replace
to identify the uppercase
and replace that.
$string = 'helloWorldAndStackOverflow';
$string = preg_replace('/([A-Z])/', '.$1', $string);
After that you can simply use strtolower
$string = strtolower($string);
to convert that into lower case.
you can use preg_replace_callback
for replace. try below code,
<?php
$string = 'helloWorldAndStackOverflow';
$dottedString = preg_replace_callback('/[A-Z]/', function($matches){
return $matches[0] = '.' . ucfirst($matches[0]);
}, $string);
echo $dottedString;
?>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.