I would like to change a string with
hello teSt woRld hEllo-woRld
to
Hello Test World Hello-World
I did something, that observes only spaces between the words. But I need it for spaces and "-" between. Can someone tell me how to achieve it ?
$words = explode(" ", $string);
foreach ( $words as $key=>$value ) {
$new .= mb_strtoupper( mb_substr( mb_strtolower( $value) , 0, 1, 'UTF-8'), 'UTF-8' ) .
mb_substr(mb_strtolower( $value ), 1, null, 'UTF-8') . ' ';
}
you should try something like this :
$string = "hello teSt woRld hEllo-woRld";
echo ucwords(strtolower($string), " -");
The second parameter of the ucwords
function is the word delimiter. In this case, words are separated with either a space or a hyphen.
The result will be :
Hello Test World Hello-World
If you are concerned about keeping the UTF-8 format, you should consider using the mb_convert_case
function like the example below :
$str = "mary had a Little lamb and she loved it so";
$str = mb_convert_case($str, MB_CASE_TITLE, "UTF-8");
echo $str; // Display: Mary Had A Little Lamb And She Loved It So