note 110002 added to function.ucwords

From: Date: Mon, 10 Sep 2012 15:41:47 +0000
Subject: note 110002 added to function.ucwords
Groups: php.notes 
Request: Send a blank email to php-notes+get-190612@lists.php.net to get a copy of this message
Features: - multi byte compatible - handles multiple delimiters function ucwords_specific ($string, $delimiters = '', $encoding = NULL) { if ($encoding === NULL) { $encoding = mb_internal_encoding();} if (is_string($delimiters)) { $delimiters = str_split( str_replace(' ', '', $delimiters)); } $delimiters_pattern1 = array(); $delimiters_replace1 = array(); $delimiters_pattern2 = array(); $delimiters_replace2 = array(); foreach ($delimiters as $delimiter) { $uniqid = uniqid(); $delimiters_pattern1[] = '/'. preg_quote($delimiter) .'/'; $delimiters_replace1[] = $delimiter.$uniqid.' '; $delimiters_pattern2[] = '/'. preg_quote($delimiter.$uniqid.' ') .'/'; $delimiters_replace2[] = $delimiter; } // $return_string = mb_strtolower($string, $encoding); $return_string = $string; $return_string = preg_replace($delimiters_pattern1, $delimiters_replace1, $return_string); $words = explode(' ', $return_string); foreach ($words as $index => $word) { $words[$index] = mb_strtoupper(mb_substr($word, 0, 1, $encoding), $encoding).mb_substr($word, 1, mb_strlen($word, $encoding), $encoding); } $return_string = implode(' ', $words); $return_string = preg_replace($delimiters_pattern2, $delimiters_replace2, $return_string); return $return_string; } Params: 1. string: The string being converted 2. delimiters: a string with all wanted delimiters written one after the other e.g. "-'" 3. encoding: Is the character encoding. If it is omitted, the internal character encoding value will be used. Example Usage: mb_internal_encoding('UTF-8'); $string = "JEAN-PAUL d'artagnan şŠ-òÀ-éÌ hello - world"; echo ucwords_specific( mb_strtolower($string, 'UTF-8'), "-'"); Output: Jean-Paul D'Artagnan Şš-Òà-Éì Hello - World ---- Server IP: 94.141.21.201 Probable Submitter: 217.133.47.244 ---- Manual Page -- http://www.php.net/manual/en/function.ucwords.php Edit -- https://master.php.net/note/edit/110002 Del: integrated -- https://master.php.net/note/delete/110002/integrated Del: useless -- https://master.php.net/note/delete/110002/useless Del: bad code -- https://master.php.net/note/delete/110002/bad+code Del: spam -- https://master.php.net/note/delete/110002/spam Del: non-english -- https://master.php.net/note/delete/110002/non-english Del: in docs -- https://master.php.net/note/delete/110002/in+docs Del: other reasons-- https://master.php.net/note/delete/110002 Reject -- https://master.php.net/note/reject/110002 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#190612) next »