note 108856 modified in function.ucfirst by danbrown

From: Date: Wed, 30 May 2012 18:21:08 +0000
Subject: note 108856 modified in function.ucfirst by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-188303@lists.php.net to get a copy of this message
This function is multilanguage. It converts only the first character of the string to uppercase (UTF-8). Other chars in string will be unchanged.
Usage: my_ucfirst("šlepetės ABC 123 abc");
// Returns Šlepetės ABC 123 abc

<?php
function substr_unicode($str, $s, $l = null) {
 return join("", array_slice(
 preg_split("//u", $str, -1, PREG_SPLIT_NO_EMPTY), $s, $l));
}

function my_ucfirst($string, $e ='utf-8') {
		$orig_string = $string;
 if (function_exists('mb_strtoupper') && function_exists('mb_substr') && !empty($string)) {
 $string = mb_strtolower($string, $e);
 $upper = mb_strtoupper($string, $e);
 preg_match('#(.)#us', $upper, $matches);
 $string = $matches[1] . mb_substr($string, 1, mb_strlen($string, $e), $e);
 } else {
 $string = ucfirst($string);
 }
		
		$string1 = substr_unicode($string, 0, 1);
		$string2 = substr_unicode($orig_string, 1);
		$string = $string1 . $string2;
		
 return $string;
 }
?> --was-- This function is multilanguage. It converts only the first character of the string to uppercase (UTF-8). Other chars in string will be unchanged. Usage: my_ucfirst("šlepetės ABC 123 abc"); // Returns Šlepetės ABC 123 abc function substr_unicode($str, $s, $l = null) { return join("", array_slice( preg_split("//u", $str, -1, PREG_SPLIT_NO_EMPTY), $s, $l)); } function my_ucfirst($string, $e ='utf-8') { $orig_string = $string; if (function_exists('mb_strtoupper') && function_exists('mb_substr') && !empty($string)) { $string = mb_strtolower($string, $e); $upper = mb_strtoupper($string, $e); preg_match('#(.)#us', $upper, $matches); $string = $matches[1] . mb_substr($string, 1, mb_strlen($string, $e), $e); } else { $string = ucfirst($string); } $string1 = substr_unicode($string, 0, 1); $string2 = substr_unicode($orig_string, 1); $string = $string1 . $string2; return $string; } http://php.net/manual/en/function.ucfirst.php

« previous php.notes (#188303) next »