PHP学习笔记与Demo(三之字符串相关)

<?php
/**
 * Created by PhpStorm.
 * User: wei19
 * Date: 2019/1/19
 * Time: 19:16
 */
/*
 * 清空字符串中的多于空格
 * chop(),ltrim(),trim()
 * trim()去除字符串开头和结尾多余的空格
 */
/*
 * 打印输出函数printf()和sprintf()
 */
$num = 20.2345;
printf("num is %.2f",$num);
/*
 * Strtoupper():将字符串转换为大写
 * Strtolower():将字符串转换为小写
 * Uefirst():如果字符串的第一个字符是字母就将该字母转换为大写
 * Ucwords():将字符串的每个单词首字母转换为大写
 * 不改变原字符串
 */
echo "<br />";
$str1 = 'wangwei wangwei wangwei WangWEI';
$str2 = Strtoupper($str1);
echo $str2;
$str3 = Strtolower($str1);
echo "<br />".$str3."<br />";
$str5 = Ucwords($str1);
echo $str5."<br />";
/*
 * 字符串的连接与分隔
 * explode():根据指定的分隔符将字符串分隔为小块返回到一个数组中
 */
$email = "[email protected]";
$email_array = explode("@",$email);
for ($i=0; $i<2; $i++){
    echo 'The value of $email_array['.$i.'] is '.$email_array[$i]."<br />";
}
//substr(string string, int start[, int length])
//字符串比较:Int strcmp(string str1, string str2),相等返回0,str1>str2返回一个正数,反之返回一个负数,区分大小写
//strcasecmp()不区分大小写
//strlen()检测字符串的长度
/*
 * 在字符串中查找字符串strstr()
 * stristr():不区分大小写
 */
$str6 = "[email protected]";
$strstr = strstr($str6,'@');
echo $strstr."<br />"; //@email.com
$stristr = stristr($str6,'wEi');
echo $stristr."<br />"; //[email protected]
/*
 * strpos():返回第一次出现子字符串的位置
 * strrpos():返回最后一次出现子字符串的位置
 */
/*
 * 正则表达式函数
 * ereg():用于查找子字符串
 * int ereg(string pattern, string search, array [matchs]);
 * 该函数搜索字符串search,在pattern中寻找与正则表达式相匹配的字符串,发现则存在数组matchs中
 * eregi()不区分大小写
 */
/*
 * ereg_replace()和eregi_replace()
 * string ereg_replace(string pattern, string replacement, string search);
 * 该函数在字符串search中查找正则表达式pattern的字符串,且用字符串replacement来替换
 * 函数eregi_replace()不区分大小写,其功能相同
 */
/*
 * 函数split():array split(string pattern, string search[, int max]);
 * 该函数将字符串search分隔成符合正则表达式模式的子字符串,然后将子字符串返回到一个数组中
 * 参数max指定数组中的元素个数
 */
$split = split('\.|@',$str6);
while (list($key, $value) = each($split)) {
    echo "<br />".$value;
}
//输出为:
/*
sunnywangweiwei
email
com
*/




猜你喜欢

转载自blog.csdn.net/qq_36595013/article/details/86563942
今日推荐