PHP 字符函数

转载:http://blog.csdn.net/dongsg11200/article/details/22053237

第一部分:关于大小写的函数。

1.strtolower(): 该函数将传入的字符串参数所有的字符都转换成小写,并以小定形式放回这个字符串.

<?php   
    $str = "Mary Had A Little Lamb and She LOVED It So";   
    $str = strtolower($str);   
    echo $str; // Prints mary had a little lamb and she loved it so   
?>

2.strtoupper(): 该函数的作用同strtolower函数相反,是将传入的字符参数的字符全部转换成大写,并以大写的形式返回这个字符串.用法同strtolowe()一样。

<?php   
    $str = "Mary Had A Little Lamb and She LOVED It So";   
    $str = strtoupper($str);   
    echo $str; // Prints MARY HAD A LITTLE LAMB AND SHE LOVED IT SO   
?>

3.ucfirst(): 该函数的作用是将字符串的第一个字符改成大写,该函数返回首字符大写的字符串.用法同strtolowe()一样. 与之类似的有lcfirst().

<?php   
    $foo = 'hello world!';   
    $foo = ucfirst($foo);             // Hello world!   
       
    $bar = 'HELLO WORLD!';   
    $bar = ucfirst($bar);             // HELLO WORLD!   
    $bar = ucfirst(strtolower($bar)); // Hello world!   
?>

4.ucwords():该函数将传入的字符串的每个单词的首字符变成大写。如"imphper cn",经过该函数处理后,将返回"Imphper Cn"。用法同strtolower()一样。

<?php   
    $foo = 'hello world!';   
    $foo = ucwords($foo);             // Hello World!    
       
    $bar = 'HELLO WORLD!';   
    $bar = ucwords($bar);             // HELLO WORLD!   
    $bar = ucwords(strtolower($bar)); // Hello World!   
?>

 第二部分:关于子字符串(字符串截取)

1.substr():返回字符串的子串。

string substr ( string $string , int $start [, int $length ] ):第一个参数为字符串,第二个参数为截取起始位置,第三个可选参数为截取的长度。

<?php
$rest = substr("abcdef", -1);    // 返回 "f"
$rest = substr("abcdef", -2);    // 返回 "ef"
$rest = substr("abcdef", -3, 1); // 返回 "d"
?>
<?php
$rest = substr("abcdef", 0, -1);  // 返回 "abcde"
$rest = substr("abcdef", 2, -1);  // 返回 "cde"
$rest = substr("abcdef", 4, -4);  // 返回 ""
$rest = substr("abcdef", -3, -1); // 返回 "de"
?>

2.substr_replace():子字符串替换。

mixed substr_replace ( mixed $string , mixed $replacement , mixed $start [, mixed $length ] )

原文地址:https://www.cnblogs.com/laining/p/7487173.html