一、strrev与array_reverse (字节或字符串的反转)
strrev() 函数用于按字节反转字符串。
print strrev('This is not a palindrome.'); 其输出为: .emordnilap a ton si sihT
array_reverse() 函数将原数组中的元素顺序翻转,创建新的数组并返回。如果第二个参数指定为 true,则元素的键名保持不变,否则键名将丢失。其语法结构为:array_reverse ($array [, bool $preserve_keys = false ] )
preserve_keys可选, 如果设置为 TRUE 会保留数字的键。 非数字的键则不受这个设置的影响,总是会被保留 。
$s = "Once upon a time there was a turtle."; // break the string up into words $words = explode(' ',$s); // reverse the array of words $words = array_reverse($words); // rebuild the string $s = implode(' ',$words); print $s; 输出结果为: turtle. a was there time a upon Once
其原理是按空格符号进行分隔的。
二、ucfirst与ucwords(小写转大写)
ucfirst()用于将字符串的首字母转换为大写,ucwords() 是将字符串中每个单词的首字母转换为大写 。这里单词的定义是紧跟在空白字符(空格符、制表符、换行符、回车符、水平线以及竖线)之后的子字符串。
例1:
<?php $foo = 'hello world!'; $foo = ucfirst($foo); // Hello world! $bar = 'HELLO WORLD!'; $bar = ucfirst($bar); // HELLO WORLD! $bar = ucfirst(strtolower($bar)); // Hello world! ?>
例2:
<?php $foo = 'hello world!'; $foo = ucwords($foo); // Hello World! ?>
三、lcfirst、strtolower、strtoupper
lcfirst — 使一个字符串的第一个字符小写;strtolower — 将字符串转化为小写;strtoupper — 将字符串转化为大写。
例1:
<?php $foo = 'HelloWorld'; $foo = lcfirst($foo); // helloWorld $bar = 'HELLO WORLD!'; $bar = lcfirst($bar); // hELLO WORLD! $bar = lcfirst(strtoupper($bar)); // hELLO WORLD! ?>
例2:
<?php $str = "Mary Had A Little Lamb and She LOVED It So"; $str = strtolower($str); echo $str; // 打印 mary had a little lamb and she loved it so ?>
例3:
<?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 ?>
四、mb_strtolower、mb_strtoupper、mb_convert_case
mb_strtolower — 使字符串小写,语法结构为:mb_strtolower ( $str [,$encoding = mb_internal_encoding() ] ) 。相较于strtolower,其增加了内部编码转换功能。和 strtolower() 不同的是,“字母”字符的检测是根据字符的 Unicode 属性。 因此函数的行为不会受语言设置的影响,能偶转换任意具有“字母”属性的字符,例如元音变音 A(Ä)———— 关于这点我也不是很明白,该话是来自官方的介绍。
例1(该例中功能和strtolower功能完全相同):
<?php $str = "Mary Had A Little Lamb and She LOVED It So"; $str = mb_strtolower($str); echo $str; // 输出: mary had a little lamb and she loved it so ?>
例2(非拉丁 UTF-8 文本的 mb_strtolower() )
<?php $str = "Τάχιστη αλώπηξ βαφής ψημένη γη, δρασκελίζει υπέρ νωθρού κυνός"; $str = mb_strtolower($str, 'UTF-8'); echo $str; // 输出 τάχιστη αλώπηξ βαφής ψημένη γη, δρασκελίζει υπέρ νωθρού κυνός ?>
mb_strtoupper和mb_strtolower用法完全相同,只不过功能上相反而已,这里不再介绍和示例。
mb_convert_case — 对字符串进行大小写转换,其语法格式为string mb_convert_case ( $str , $mode [, $encoding = mb_internal_encoding() ] )
str要被转换的 string ; mode转换的模式。它可以是 MB_CASE_UPPER、 MB_CASE_LOWER 和 MB_CASE_TITLE 的其中一个 ; encoding 参数为字符编码。如果省略,则使用内部字符编码。
其中MB_CASE_UPPER为转换所有字节为大写, MB_CASE_LOWER为转换所有字节为小写, MB_CASE_LOWER转换所有单词的首字母为大写。例:
<?php $str = "MAry had A Little lamb and she loved it so"; $str = mb_convert_case($str, MB_CASE_UPPER, "UTF-8"); echo $str; // 输出 MARY HAD A LITTLE LAMB AND SHE LOVED IT SO echo "<br/>"; $str = mb_convert_case($str, MB_CASE_TITLE, "UTF-8"); echo $str; // 输出 Mary Had A Little Lamb And She Loved It So echo "<br/>"; $str = mb_convert_case($str, MB_CASE_LOWER, "UTF-8"); echo $str; //输出 mary had a little lamb and she loved it so ?>