note 110002 modified in function.ucwords by danbrown
| From: | danbrown@php.net | Date: | Wed, 12 Sep 2012 17:29:54 +0000 |
| Subject: | note 110002 modified in function.ucwords by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-190665@lists.php.net to get a copy of this message | ||
Features: - multi byte compatible - handles multiple
delimiters <?php 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: <?php 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
--was--
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
http://php.net/manual/en/function.ucwords.php