strtoupper()

(PHP 4, PHP 5, PHP 7)

将字符串转化为大写

说明strtoupper(string$string) :string

将$string中所有的字母字符转换为大写并返回。

注意“字母”与当前所在区域有关。例如,在默认的“C”区域,字符 umlaut-a(ä)就不会被转换。

参数$string输入字符串。

返回值

返回转换后的大写字符串。

范例

Example #1strtoupper()范例<?php

$str = "Mary Had A Little Lamb and She LOVED It So";

$str = strtoupper($str);

echo $str; // 打印 MARY HAD A LITTLE LAMB AND SHE LOVED IT SO

?>

注释Note:此函数可安全用于二进制对象。

参见One might think that setting the correct locale would do the trick with for example german umlauts, but this is not the case. You have to use mb_strtoupper() instead:

setlocale(LC_CTYPE, 'de_DE.UTF8');

echo strtoupper('Umlaute äöü in uppercase'); // outputs "UMLAUTE äöü IN UPPERCASE"

echo mb_strtoupper('Umlaute äöü in uppercase', 'UTF-8'); // outputs "UMLAUTE ÄÖÜ IN UPPERCASE"

?>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).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");It has been mentioned in a previous comment that all you need to do to let PHP's strtoupper() do the conversion - instead of writing more or less complicated functions yourself - is to specify the locale in which you're doing the case conversion:

It is important to note that setlocale() will silently fail if it can't find the specified locale on your system, so *always* check its return value. Try different spellings: using "de_AT" as an example, there are various combinations that may or may not work for you: "de", "de_AT.utf8", "de_AT.iso-8859-1", "de_AT.latin1", "de_AT@euro", etc).

If you can't find an appropriate locale setting, check your system configuration (locales are a system-wide setting, PHP gets them from the OS). On Windows, locales can be set from the Control Panel; on Linux it depends on your distribution. You can try "sudo dpkg-reconfigure locales" on Debian-based distros, or configure them manually. On Ubuntu Dapper, I had to copy entries over from /usr/share/i18n/SUPPORTED to /var/lib/locales/supported.d/local, then do the dpkg-reconfigure.

After you're done, restart the web server.

That said, there are special cases where you want to do the conversion manually. In German, for example, the letter 'ß' (szlig) only exists as a lower-case character, and so doesn't get converted by strtoupper. The convential way to express a 'ß' in an uppercase string is "SS". This function will take care of this exception (for Latin1 and most of Latin9, at least):

define("LATIN1_UC_CHARS", "ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖØÙÚÛÜÝ");

define("LATIN1_LC_CHARS", "àáâãäåæçèéêëìíîïðñòóôõöøùúûüý");

function uc_latin1 ($str) {

$str = strtoupper(strtr($str, LATIN1_LC_CHARS, LATIN1_UC_CHARS));

return strtr($str, array("ß" => "SS"));

}

?>// 2005/5/30 Justin

// Chinese_Traditional toupper

function CT_to_upper($string)

{

$isChineseStart = false;

$new_string = "";

$i = 0;

while($i < strlen($string))

{

if (ord(substr($string,$i,1)) <128)

{

if( $isChineseStart == false )

$new_string .= strtoupper(mb_substr($string,$i,1));

else

$new_string .= substr($string,$i,1);

}

else

{

if( $isChineseStart == false )

$isChineseStart = true;

else

$isChineseStart = false;

$new_string .= substr($string,$i,1);

}

$i++;

}

return $new_string;

}

//When using UTF-8 and need to convert to uppercase with

special characters like the german ä,ö,ü (didn't test for french,polish,russian but think it should work, too) try this:

function strtoupper_utf8($string){

$string=utf8_decode($string);

$string=strtoupper($string);

$string=utf8_encode($string);

return $string;

}Simple function to change the case of your string and any accented html characters contained within it.

Inspired by fullUpper(), by silent at gmx dot li... just a little bit more atomic.

function convertCase($str, $case = 'upper')

{ //yours, courtesy of table4.com :)

switch($case)

{

case "upper" :

default:

$str = strtoupper($str);

$pattern = '/&([A-Z])(UML | ACUTE | CIRC | TILDE | RING | ';

$pattern .= 'ELIG | GRAVE | SLASH | HORN | CEDIL | TH);/e';

$replace = "'&'.'\\1'.strtolower('\\2').';'"; //convert the important bit back to lower

break;

case "lower" :

$str = strtolower($str);

break;

}

$str = preg_replace($pattern, $replace, $str);

return $str;

}

?>

Depending on what you are trying to achieve you would call like this:

//with entities...

$str = convertCase(htmlentities($str, ENT_QUOTES, "ISO-8859-1"));

?>In the Irish language certain initial mutations can never be capitalized — the following simple function can be used to capitalize text in Irish.

i.e. Muintir na hÉireann -> MUINTIR NA hÉIREANN

function strtoupper_ga($a) {

return strtr(mb_strtoupper($a, "utf-8"), array(

" MB" => " mB",

" GC" => " gC",

" ND" => " nD",

" BHF" => " bhF",

" NG" => " nG",

" BP" => " bP",

" DT" => " dT",

" HA" => " hA",

" HE" => " hE",

" HI" => " hI",

" HO" => " hO",

" HU" => " hU",

" HÁ" => " hÁ",

" HÉ" => " hÉ",

" HÍ" => " hÍ",

" HÓ" => " hÓ",

" HÚ" => " hÚ"

));

}

?>perfect solutions for turkish utf-8 (including i I conversations):

function strtolowertr($metin){

return mb_convert_case(str_replace('I','ı',$metin), MB_CASE_LOWER, "UTF-8");

}

function strtouppertr($metin){

return mb_convert_case(str_replace('i','İ',$metin), MB_CASE_UPPER, "UTF-8");

}

function ucwordstr($metin) {

return ltrim(mb_convert_case(str_replace(array(' I',' ı', ' İ', ' i'),array(' I',' I',' İ',' İ'),' '.$metin), MB_CASE_TITLE, "UTF-8"));

}

function ucfirsttr($metin) {

$metin = in_array(crc32($metin[0]),array(1309403428, -797999993, 957143474)) ? array(strtouppertr(substr($metin,0,2)),substr($metin,2)) : array(strtouppertr($metin[0]),substr($metin,1));

return $metin[0].$metin[1];

}

?>Russian

function str_to_upper($str){

return strtr($str,

"abcdefghijklmnopqrstuvwxyz".

"\xE0\xE1\xE2\xE3\xE4\xE5".

"\xb8\xe6\xe7\xe8\xe9\xea".

"\xeb\xeC\xeD\xeE\xeF\xf0".

"\xf1\xf2\xf3\xf4\xf5\xf6".

"\xf7\xf8\xf9\xfA\xfB\xfC".

"\xfD\xfE\xfF",

"ABCDEFGHIJKLMNOPQRSTUVWXYZ".

"\xC0\xC1\xC2\xC3\xC4\xC5".

"\xA8\xC6\xC7\xC8\xC9\xCA".

"\xCB\xCC\xCD\xCE\xCF\xD0".

"\xD1\xD2\xD3\xD4\xD5\xD6".

"\xD7\xD8\xD9\xDA\xDB\xDC".

"\xDD\xDE\xDF");

}Here is how to make the character in upper case, except HTML-entities:

$s = substr(preg_replace('/(?<=^ | ;)(.+?)(?=&[0-9A-Za-z]+; | $)/e', "strtoupper('$1')", ' '.$s), 1);

There is small kludge, however. Unfortunately I tired to find out the way how to exclude HTML-entity at the start of the line, so I have added 1 dummy character at the start of the text and removing it after the conversion.This function has a real challenge when it comes to Turkish. In Turkish the ASCII letter 'i' uppercases to a non-ASCII character. This means that PHP cannot upper case it.

i.e. for the Turkish locale, strtoupper('i')=='i'

This can mess with basic program logic.

There's no simple solution. The core problem is discussed more here:

http://www.i18nguy.com/unicode/turkish-i18n.html$ther=''.THEREISALREADYA.' '.CONCEPT.' '.SAVED.' ';

or

$ther="There Is all ready A concept SAVED";

$fupper=substr("$ther",0,1);

pick the first char

$theru = strtoupper($fupper);

make it upper

$flower=substr("$ther",1,100);

pick the rest

$therl = strtolower($flower);

make them lower

Result:

There is all ready a concept savedchinese

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));

$i++;

} else {

$new_string .= substr($string,$i,2);

$i=$i+2;

}

}

return $new_string;

}accents convertion trick :

function ucfirstHTMLentity($matches){

return "&".ucfirst(strtolower($matches[1])).";";

}

function fullUpper($str){

$subject = strtoupper(htmlentities($str, null, 'UTF-8'));

$pattern = '/&([A-Z]+);/';

return preg_replace_callback($pattern, "ucfirstHTMLentity", $subject);

}

print fullUpper($_REQUEST["txt"]);

?>Why this function isn't just named "uppercase" or has an alias named that, I don't know.

php7 字符串转数字,strtoupper()相关推荐

  1. php 参数与数字比较,PHP8中字符串与数字的比较更智能

    PHP8中字符串与数字的比较更智能 PHP8.0发布[1]也有一段时间了,此次发布带来了很多实用且强大的功能,比如: Named arguments // php 7.x htmlspecialcha ...

  2. python字符串拼接数字_python字符串和数值操作函数大全(非常全)

    字符串和数值型数字的操作大全 1.反斜杠\的使用规则:一般使用表示续行的操作,可以其他符号相结合组成其他的一些使用符号,转义字符\'的使用会识别引号,使得字符串中的引号和外面本来的啊引号相区分. (1 ...

  3. iOS 改变字符串中数字的颜色

    匹配中文字符 [\u4e00-\u9fa5] 匹配双字节字符(包括汉字在内) [^\x00-\xff] 匹配网址:[a-zA-z]+://[^\s]* 匹配国内电话 \d{3}-\d{8}|\d{4} ...

  4. SCRIPT1028:缺少标识符、字符串或数字 jquery ajax

    2019独角兽企业重金招聘Python工程师标准>>> SCRIPT1028:缺少标识符.字符串或数字 使用jquery时报此错误 究其原因是对象键值对格式错误: 原格式:   多了 ...

  5. pandas使用read_csv读取文件数据、设置converters参数将百分比字符串转换为数字

    pandas使用read_csv读取文件数据.设置converters参数将百分比字符串转换为数字 目录 pandas使用read_csv读取文件数据.设置converters参数将百分比字符串转换为 ...

  6. C/C++中字符串与数字之间的转换

    主要有两种方式:C 中能够使用 sprintf 将数字转为字符数组,sscanf 将字符数组转为数字:而在 C++ 中不仅能够使用 C 中的方法,还能够使用 stringstream 实现字符串与数字 ...

  7. 如何用计算机猜数字,杭电2010计算机复试笔试题 2道acm简单题(2010):1.猜数字游戏;2.字符串提取数字并求和;...

    //第一题是猜数字的游戏. //题目:随即产生一个3位的正整数,让你进行猜数字, //如果猜小了,输出:"猜小了,请继续". //如果猜大了,输出:"猜大了,请继续&qu ...

  8. js 判断是不是数字||判断字符串是不是数字(正则表达式)

    js使用正则表达式判断对象是不是数字,或者字符串是不是数字,或者是不是数字类型 1 //判断是不是一个数字 或者 一个字符串里全是数字 2 isNumber (value) { 3 if (value ...

  9. 判断字符串是不是数字

    在网上看到一篇关于判断字符串是数字的函数, 感觉思路不错, 代码简洁, 我转载一下: 代码 public static bool isnum(string xtext) //判断字符串是不是数字    ...

最新文章

  1. python资料免费-python 资料
  2. Shell脚本基本命令4
  3. Java Streams,第 2 部分: 使用流执行聚合
  4. Android界面编程--使用活动条(ActionBar)--通过ActionBar菜单改变TextView的字体和颜色...
  5. python之torchlight使用_python游戏编程之pgzero使用介绍
  6. Python——分布式监控项目
  7. 3d激光雷达开发(入门)
  8. Spring Boot 表单验证
  9. java设计模式--基础思想总结--抽象类与架构设计思想
  10. 基于STM32的DS1302时钟模块驱动程序
  11. TVS管与稳压二极管的区别
  12. 磁盘容量超过64T分配单元大小需要设置64K
  13. 【每日一GO】加密解密库—dongle
  14. [词一首]【相思难断】
  15. for循环,for...in循环,forEach循环的区别
  16. 鲲鹏生态跑出加速度 中原数字经济再续新动能
  17. 双向可控硅过零触发电路
  18. 微软Google等互联网公司经典面试智力题和解答
  19. python基础语法条件判断基础题训练
  20. 第24篇-极某壁纸结果参数分析

热门文章

  1. 看守所里的信息化故事:刘所家的新地毯
  2. redis expire命令
  3. SpringBoot 集成FluentMyBatis 框架之集成分页功能
  4. LeetCode琅琊榜第九层-加油站问题(图表法)
  5. 点选识别DLL/滑块识别DLL/通用验证码识别DLL/图标点选/本地识别DLL
  6. linux底层把值传给上层,Android上层如何调用一个底层函数
  7. 在Chrome安装Edge的插件
  8. 用c语言实现字符大小写转化
  9. 用C语言实现的简单Web服务器(Linux)
  10. 电商双十一购物节促销活动及短信营销解决方案