字符串长度函数
//strlen-获取字符串长度
$str = 'time is yes';
echo strlen($str); //11-按字节算,空格也算。一个中文UTF-8是三个字节。GBK是两个字节
//mb_strlen-获取字符串长度
$str = 'time is yes';
$str1 = '中国';
echo mb_strlen($str);//11
echo "
";
echo mb_strlen($str1 , 'utf8');//2
echo "
";
echo mb_strlen($str1 , 'gbk');//3
//strpos-查找字符的首次出现位置,区分大小写
$str = 'time is yes TIME';
echo strpos($str , 'T');//12,查找首次出现的位置,区分大小写
echo strpos($str , 's');//6
echo "
";
//stripos-查找字符的首次出现位置,不区分大小写
$str = 'time is yes TIME';
echo stripos($str , 'T');//0,查找首次出现的位置,不区分大小写
echo stripos($str , 'I');//1
echo "
";
//strrpos-查找字符最后一次出现的位置,区分大小写
$str = 'time is yes TIMES';
echo strrpos($str , 's');//10
echo "
";
字符串替换函数
//str_replace-查找并替换字符串
$str = 'time is yes TIMES';
echo str_replace('yes' , 'no' , $str);//time is no TIMES
echo "
";
//str_ireplace-查找并替换字符串
echo str_ireplace('TIMES' , 'times' , $str);//time is yes times
echo "
";
//strtr-函数转换字符串中特定的字符
$str2 = '男孩,女孩,男人,女人';
echo $str2;//男孩,女孩,男人,女人
echo "
";
echo strtr($str2 , array('男'=>'女' , '女'=>'男'));//女孩,男孩,女人,男人
echo "
";
//substr_replace-替换字符串的子串
$str = 'time is yes TIMES';
echo substr_replace($str , 'nopapu' , 12);//time is yes nopapu
截取字符串函数
//substr-截取字符串
$str = 'time is yes TIMES';
echo substr($str , 7);//yes TIMES
echo "
";
echo substr($str , 7 , 4);//yes
echo "
";
//strstr-查找字符串的首次出现,截取,区分大小写
$str = 'time is yes TIMES';
echo strstr($str , 'is');//is yes TIMES
echo "
";
//stristr-查找字符串的首次出现,截取,不区分大小写
$str = 'time is yes TIMES';
echo stristr($str , 'TIME'); //time is yes TIMES
//strrchr-从最后一次搜索到的字符处返回
echo "
";
$str = 'time is yes TIMES';
echo strrchr($str , 's');//s TIMES
分割、连接、反转函数
//str_split-把$str 按 len 长度进行分割返回数组
$str = 'time is yes TIMES';
//把字符串以5为单位分割成数组单元
print_r(str_split($str , 5));//Array ( [0] => time [1] => is ye [2] => s TIM [3] => ES )
echo "
";
//explode-搜索字符串并取出,结果返回一个数组
$str = 'time is yes TIMES';
print_r(explode($str , 'is'));//Array ( [0] => is )
print_r(explode($str , 'TIME'));//Array ( [0] => TIME )
echo "
";
//implode-把数组值连接成字符串
$arr = array('d','b','q','i','l','y');
echo implode('' , $arr);//dbqily
空白处理函数
//trim-去除字符串首尾处的空白字符(或者其他字符)
echo "
";
$str = '\n\n\n\n\ntime is yes TIMES ';
echo $str;//\n\n\n\n\ntime is yes TIMES
$s = '\n';
echo "
";
echo trim($str , $s);//time is yes TIMES
//ltrim-删除字符串开头的空白字符(或者其他字符)
$str = '\n\n\n\n\ntime is yes TIMES ';
echo "
";
echo ltrim($str , $s);//time is yes TIMES
echo "
";
//rtrim-删除字符串末端的空白字符(或者其他字符)
$str = 'time is yes TIMES\n\n\n\n\n';
echo rtrim($str , $s);//time is yes TIMES
echo "
";
//chunk_split-向字符里面按N个字符就加入一个空格;
$str = 'timeisyesTIMES\n\n\n\n\n';
echo chunk_split($str , 4);//time isye sTIM ES\n \n\n \n\n
//str_pad
$st = 'time';
$str = ' is no';
echo "
";
echo str_pad($st , 10 , $str);//time is no
echo "
";
//默认在左边填充两个字节
echo str_pad($st , 10 , $str , STR_PAD_BOTH);//istime is
echo "
";
//将字符串全部放到左边
echo str_pad($st , 10 , $str , STR_PAD_LEFT);//is notime
字符转义函数
//addslashes-转义特殊字符
$str = "Is your name O'reilly?";
// 输出: Is your name O\'reilly?
echo addslashes($str);
echo "
";
//stripslashes-去掉转义字符
$str = "Is your name O\'reilly?";
// 输出: Is your name O'reilly?
echo stripslashes($str);
echo "
";
//htmlspecialchars-将特殊字符转换为 HTML 实体
$new = htmlspecialchars("Test", ENT_QUOTES);
echo $new; // <a href='test'>Test</a>
echo "
";
//htmlspecialchars_decode —— htmlspecialchars()函数的反函数, 将 HTML 实体转换为字符
$new = "<a href='test'>Test</a>";
echo htmlspecialchars_decode($new);//超链接test
echo "
";
//html_entity_decode —— htmlentities ()函数的反函数, 将 HTML 实体转换为字符
$new = "<a href='test'>Test</a>";
echo html_entity_decode($new);//超链接test
echo "
";
//htmlentities —— 将字符串中所有可转换字符转换为 HTML 实体
$str = "A 'quote' is bold";
echo htmlentities($str);// A 'quote' is <b>bold</b>
echo "
";
echo htmlentities($str, ENT_QUOTES);//A 'quote' is <b>bold</b>
字符串比较函数
//int strcmp($str1,$str2):(字符串比较)
$var1 = "Hello";
$var2 = "hello";
if (strcmp($var1, $var2) !== 0) {//区分大小写,var1和var2不相等
echo "$var1 和 $var2 不相等";
}
//strcasecmp() 同上(不分大小写)
echo "
";
$var1 = "Hello";
$var2 = "hello";
if (strcasecmp($var1, $var2) == 0) {//因不区分大小写,var1和var2相等
echo "$var1 和 $var2 相等";
}