Skip to content

Instantly share code, notes, and snippets.

@burakerdem
Created June 4, 2011 01:38
Show Gist options
  • Save burakerdem/1007450 to your computer and use it in GitHub Desktop.
Save burakerdem/1007450 to your computer and use it in GitHub Desktop.
PHP: ucwords() for UTF-8 strings
<?php
if (!function_exists('mb_ucwords'))
{
function mb_ucwords($str)
{
return mb_convert_case($str, MB_CASE_TITLE, "UTF-8");
}
}
@rrelmy
Copy link

rrelmy commented Jul 4, 2017

mb_convert_case does not match the behaviour of ucwords?

Code Output
ucwords('fooBar') FooBar
mb_convert_case('fooBar', MB_CASE_TITLE) Foobar

Note the lowercased b!

@wrabit
Copy link

wrabit commented Apr 15, 2020

It is not a replacement for ucwords

@vuhanguyen
Copy link

Thanks, it helps me solve the problem of ucwords with Vietnamese UTF-8 strings

@francescom
Copy link

francescom commented Nov 7, 2022

You have to mb_strtolower, before.

<?php

if (!function_exists('mb_ucwords'))
{
	function mb_ucwords($str,$encoding='UTF-8')
	{
		return mb_convert_case(mb_strtolower($str, $encoding), MB_CASE_TITLE, $encoding);
	}
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment