note 102033 deleted from function.ucwords by danbrown

From: Date: Mon, 24 Jan 2011 15:54:13 +0000
Subject: note 102033 deleted from function.ucwords by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-176249@lists.php.net to get a copy of this message
Note Submitter: e-mike ---- I used "dyler85" code and tuned it some more, also encoding ready. <?php if ( ! function_exists('mb_ucwords')) { /** * Uppercase the first character of each word in a string * * $str = "côte d'ivoire mcdonalds hobby's 's avonds s'il vous plaît jean-luke picard füße foo'bar o'foo (bar) [bar] {bar}"; * $str = mb_strtolower($str, 'UTF-8'); * echo mb_ucwords($str, 'UTF-8'); * "Côte d'Ivoire McDonalds Hobby's 's Avonds S'il Vous Plaît S'ill Jean-Luke Picard Füße Foo'bar O'Foo (Bar) [Bar] {Bar}" * * @param string $str required * @param string $encoding optional, default mb_internal_encoding * @return string */ function mb_ucwords($str, $encoding = '') { empty($encoding) and $encoding = mb_internal_encoding(); $str = mb_convert_case($str, MB_CASE_TITLE, $encoding); $str = preg_replace( '/(?: ^ | \\b )(( [D]\' )|( \w{1}\' | Ma?c | Fitz ))([^\W\d_])([^il])/xe', "mb_strtolower('\$2', 'UTF-8').'\$3'.mb_strtoupper('\$4', 'UTF-8').'\$5'", $str); return $str; } } ?>

« previous php.notes (#176249) next »