|
|
 |
strtoupper (PHP 3, PHP 4, PHP 5) strtoupper -- Make a string uppercase Descriptionstring strtoupper ( string string )
Returns string with all alphabetic
characters converted to uppercase.
Note that 'alphabetic' is determined by the current locale. For
instance, in the default "C" locale characters such as umlaut-a
(ä) will not be converted.
Example 1. strtoupper() example |
<?php
$str = "Mary Had A Little Lamb and She LOVED It So";
$str = strtoupper($str);
echo $str; ?>
|
|
Note: This function is
binary-safe.
See also strtolower(), ucfirst(),
ucwords() and mb_strtoupper().
User Contributed Notes
strtoupper
12-Mar-2005 07:08
Ah, the last code were spoiled, here is the fixed one:
<?php
function str_to_upper($str){
return strtr($str,
"abcdefghijklmnopqrstuvwxyz".
"\x9C\x9A\xE0\xE1\xE2\xE3".
"\xE4\xE5\xE6\xE7\xE8\xE9".
"\xEA\xEB\xEC\xED\xEE\xEF".
"\xF0\xF1\xF2\xF3\xF4\xF5".
"\xF6\xF8\xF9\xFA\xFB\xFC".
"\xFD\xFE\xFF",
"ABCDEFGHIJKLMNOPQRSTUVWXYZ".
"\x8C\x8A\xC0\xC1\xC2\xC3\xC4".
"\xC5\xC6\xC7\xC8\xC9\xCA\xCB".
"\xCC\xCD\xCE\xCF\xD0\xD1\xD2".
"\xD3\xD4\xD5\xD6\xD8\xD9\xDA".
"\xDB\xDC\xDD\xDE\x9F");
}
?>
So, this function changes also other letters into uppercase, strtoupper() does only change: a-z to: A-Z.
12-Mar-2005 12:18
This function changes also other letters into uppercase, strtoupper() does only change: a-z to: A-Z.
<?php
function str_to_upper($str){
return strtr($str, "abcdefghijklmnopqrstuvwxyz", "ABCDEFGHIJKLMNOPQRSTUVWXYZޟ");
}
?>
WM-Joep
12-Dec-2004 12:26
you can make a 'key-generator' with strtoupper too!
function MaakCode($lengte=10) {
// De tekens die voorkomen in de code
$tekens = array(1, 2, 3, 4, 5, 6, 7, 8, 9, 0);
$code = '';
for($i = 1; $i <= $lengte; $i++)
$code .= $tekens[rand(0,10)];
return strtoupper($code);
30-Oct-2004 05:23
If you only need to extend the conversion by the characters of a certain language, it's possible to control this using an environment variable to change the locale:
setlocale(LC_CTYPE, "de_DE");
Luke (webmaster--@--zeyus--dot--com)
14-May-2004 03:18
Just a comment on Jose Roberto Kerne's script (which should work perfectly), it should be able to be cut down to this, as str_replace accepts array values (as of ??? php version), the only difference is this should be quicker and obviously array values could be replaced with any characters (or sets of strings) you want to be replaced
<?php
function stringUpDown($text, $up = true){
$arrayLower=array(''
,'','','','',''
,'','','',''
,'','','',''
,'','','','',''
,'','','','');
$arrayUpper=array(''
,'','','','',''
,'','','',''
,'','','',''
,'','','','',''
,'','','','');
if($text == ''){
return $text;
}
if($up != true) {
$text=strtolower($text);
$text=str_replace($arrayUpper, $arrayLower, $text);
}else{
$text=strtoupper($text);
$text=str_replace($arrayLower, $arrayUpper, $text);
}
return($text);
} ?>
p dot thomas at inlive dot info
02-Apr-2004 06:02
Some bench :
String Copy, OUT=IN : 21.398067474365 ms
String TRANSFORMATION :
- strtolower : 383.09001922607 ms
- strtolower( strtr) : 267.36092567444 ms
- preg_replace : 16624.928951263 ms
- stringUpDown : 4013.0908489227 ms
IN : jehrjzh r''_- &(r&) EAZREZREZ^m
OUT : jehrjzh r''_- &(r&) eazrezrez^m
Platform : AMD 1 Ghz, Win2K, EasyPHP
markku dot uttula at disconova dot com
15-Feb-2004 11:40
Martine's note is a good one. Taking you have mbstring compiled in, which many don't. It also seems to be missing from (at least) the Win32 binary release (which is understandable).
Since this is a problem with many languages, native one of yours truly included, I and a co-worker spent some time with this thing and came up with the following - pretty much same sort of thing many others appear to have thought out earlier. It's the fastest we could come up with (which is extreamely important if you need to use these a lot on a heavily used system), and we sincerely hope it's of use to somebody.
<?php
define( "UC_CHARS", "؊ݎ" ); define( "LC_CHARS", "" ); define( "WORD_SEPARATORS", chr(9).chr(10).chr(11).chr(12).chr(13).chr(32) );
function strtolower2($value) {
return (
strtolower(
strtr( $value, UC_CHARS, LC_CHARS )
)
);
}
function strtoupper2($value) {
return (
strtoupper(
strtr( $value, LC_CHARS, UC_CHARS )
)
);
}
function ucfirst2($value) {
return (
strtoupper2(
substr( $value, 0, 1 )
).strtolower2(
substr( $value, 1, strlen( $value ) )
)
);
}
function ucwords2( $value ) {
$upper = strtoupper2( $value );
$value = ucfirst2( $value );
$word_separators = WORD_SEPARATORS;
$len = strlen( $value ) - 1;
for ( $i = 0; $i < strlen( $word_separators ); $i++ ) {
$separator = $word_separators[$i];
$pos = -1;
while ( $pos !== false ) {
$pos = strpos( $value, $separator, ( $pos + 1 ) );
if ( ( $pos !== false ) && ( $pos < $len ) ) {
$value[ ( $pos + 1 ) ] = $upper[ ( $pos + 1 ) ];
}
}
}
return ($value);
}
?>
martine
06-Feb-2004 09:41
This may save you time and effort (if you need to convert european languages such as Czech, Portugees, German or Swedish)
the function mb_strtoupper() converts all accented characters in the latin alphabet, ie. , , , etc. This is easier than some of the suggestions below. It should also convert case properly for russian, etc.
Ansis [ataols in latnet point lv]
27-Jan-2004 05:40
In the case transformations it is better to use "preg_replace", not "strtr":
1) it is faster;
2) it is possible to write a transformation formula for all Windows codepages, instead of each letter writing as most of you did.
Thus, for "to upper case" transformation:
<?php
$result = preg_replace("/([a-z\xE0-\xFF])/e","chr(ord('\\1')-32)",$arg);
?>
For "to lower case" transformation:
<?php
$result = preg_replace("/([A-Z\xC0-\xDF])/e","chr(ord('\\1')+32)",$arg);
?>
dnlzsy at yahoo dot com
04-Apr-2003 12:28
chinese
function to_upper($string) {
$new_string = "";
$i = 0;
while($i < strlen($string)) {
if (ord(substr($string,$i,1)) <128)
{
$new_string .= strtoupper(substr($string,$i,1));
} else {
$new_string .= substr($string,$i,1);
}
$i++;
}
return $new_string;
}
Jose Roberto Kerne <kerne at kerne dot org>
17-Mar-2003 01:12
User this code for transform string in uppercase/lowercase for Portuguese Brazilian language :
function stringUpDown($text, $return){
# Convert values from Lower to Upper
$arrayLower=array(''
,'','','','',''
,'','','',''
,'','','',''
,'','','','',''
,'','','','');
$arrayUpper=array(''
,'','','','',''
,'','','',''
,'','','',''
,'','','','',''
,'','','','');
if($return=='lower') {
$text=strtolower($text);
for($i=0;$i<count($arrayLower);$i++) {
$text=str_replace($arrayUpper[$i], $arrayLower[$i], $text);
}
}
elseif($return=='upper') {
$text=strtoupper($text);
for($i=0;$i<count($arrayLower);$i++) {
$text=str_replace($arrayLower[$i], $arrayUpper[$i], $text);
}
}
return($text);
} #end of function
Angus Lord
12-Mar-2003 05:51
This function works better with html enities
function to_upper($string) {
$new_string = "";
while(eregi("^([^&]*)(&)(.)([a-z0-9]{2,9};|&)(.*)",$string,$regs)) {
$new_string .= strtoupper($regs[1]).$regs[2].strtoupper($regs[3]).$regs[4];
$string = $regs[5];
}
$new_string .= strtoupper($string);
return $new_string;
}
metallica at zoznam dot sk
09-Dec-2002 03:28
I just want to add to my note above that if you want to see the function definitions correctly, change Encoding in your browser's menu to "Central European (Windows)" [a.k.a. "Windows-1250"], otherwise you might see just a bunch of strange characters (depends on what are the defaults of your browser).
metallica at zoznam dot sk
03-Dec-2002 08:14
To Czech and Slovak programmers using Central Europe character sets Windows-1250 or ISO-8859-2:
If you're authoring the scripts on Windows and hosting zour site on a Linux based server, you might see unsatisfying results using functions strtoupper() and strtolower(). You can use these two functions instead:
function cs_strtoupper($my_string) {
return strtr( strtoupper($my_string), "", "ͼ؊ݎ" );
}
function cs_strtolower($my_string) {
return strtr( strtolower($my_string), "ͼ؊ݎ", "" );
}
mec at stadeleck dot org
02-Dec-2002 09:15
here is a "quick and dirty" strtoupper for strings containing html entities:
// string to upper case
// without transforming html entities
function to_upper($text)
{
$new_string = "";
while(eregi("^([^&]*)(&[a-z0-9]{2,9};|&)(.*)",$text,$regs))
{
$new_string .= strtoupper($regs[1]);
$new_string .= (substr($regs[2],-4)=="uml;") ? substr($regs[2],0,1) . strtoupper(substr($regs[2],1,1)) . substr($regs[2],-4) : $regs[2];
$text = $regs[3];
}
$new_string .= strtoupper($text);
return $new_string;
}
mec at stadeleck dot org
02-Dec-2002 08:54
something I myself first not thought about:
if there are any html entities (named entities) in your string, strtoupper will turn all letters within this entities to upper case, too. So if you want to manipulate a string with strtoupper it should contain only unicode entities (if ever).
mike at mikereid dot net
03-Jul-2002 08:31
rze is completely correct.
ucwords does EXACTLY what I needed!! If only I knew of the ucwords command BEFORE I made the post (and took the time to script the example two above.)
As a note, from testing, the internal ucwords and coded strtotitle command are identical, as far as output is concerned. As for rze's comment, speed is definitely faster using ucwords. I would advise to follow the link above, and see the examples.
urudz at strategma dot bg
21-Apr-2002 11:49
on linux
php gets LC_LOCAL env variable therefor you must set this
export LC_ALL=bg_BG.CP1251
export LANG=bg_BG.CP1251
before starting of apache i have put this to lines in /etc/rc.d/rc.httpd
-----
cat /etc/rc.d/rc.httpd
#!/bin/sh
#
# Start the Apache web server
#
export LC_ALL=bg_BG.CP1251
export LANG=bg_BG.CP1251
case "$1" in
'start')
/usr/sbin/apachectl startssl ;;
'stop')
/usr/sbin/apachectl stop ;;
'restart')
/usr/sbin/apachectl restart ;;
*)
echo "usage $0 start|stop|restart" ;;
esac
-------
in windows you must define your "locale"
in control panel > regional options > general
best regards urudz :>
developer at i-space dot org
26-Feb-2002 03:59
in addition to the above note :
$upper_term = strtr(strtoupper($term), "", "");
$lower_term = strtr(strtolower($term), "", "");
developer at i-space dot org
26-Feb-2002 03:48
use this to convert to cyrillic (bulgarian) upper and lower version of your string :
$term = "";
$upper_term = strtr($term, "", "");
$lower_term = strtr($term, "", "");
steve at dom dot de
28-Oct-1999 02:04
If you are unable to change your default config to support special chars such as umlauts, then use strtr to translate upper/lower versions.
<pre>$string=strtr("","");</pre>
| |