There may or may not be a shortcut method, and/or a more efficient way of putting together existing methods, but this works:
PHP Code:
<?php
$string = 'OneTwoThree';
$shortstrings = array(substr($string, 0, 1), substr($string, 1));
$shortstrings[1] = preg_replace("/([A-Z])/", "-$1", $shortstrings[1]);
$newstring = implode('', $shortstrings);
echo $newstring;
?>
If you're turning it into a function, you should test to make sure $string is a string and has a strlen of at least 2. Like:
PHP Code:
<?php
$mystring = 'OneTwoThree';
function hypenatecaps($string){
if(!is_string($string)){return;}
if(strlen($string) < 2){
return $string;
}
$shortstrings = array(substr($string, 0, 1), substr($string, 1));
$shortstrings[1] = preg_replace("/([A-Z])/", "-$1", $shortstrings[1]);
$newstring = implode('', $shortstrings);
return $newstring;
}
echo hypenatecaps($mystring);
?>
This also works:
PHP Code:
<?php
$string = 'OneTwoThree';
echo substr($string, 0, 1) . preg_replace("/([A-Z])/", "-$1", substr($string, 1));
?>
Bookmarks