发布时间:2020/04/25 作者:天马行空 阅读(936)
//按照指定长度对字符串进行折行处理
$str = "Supercalifragulistic"; $res = wordwrap($str,5,"\n",true); print_r($res);
//计算指定字符串在字符串中出现的次数
$res = substr_count("I love Shanghai. Shanghai is the biggest city in china.","Shanghai"); print_r($res);
//截取字符串
$res = substr("Hello world",6);
//把字符串中的字符 "ia" 替换为 "eo",注意是字符,不是整个字符串
$res = strtr("Hilla Warld","ia","eo"); $arr = array("Hello" => "Hi", "world" => "earth"); $res = strtr("Hello world",$arr);//以数组的方式进行替换 print_r($res);
//大小写转换
$res = strtolower("Hello WORLD.");//把所有字符转换为小写 $res = strtoupper("Hello WORLD.");//把所有字符转换为大写 print_r($res);
//通过分隔符,逐一分隔字符串
$string = "Hello world. Beautiful day today."; $token = strtok($string, " "); while ($token !== false) { echo "$token "; $token = strtok(" "); }
//str1中第一部分全部的连续字符串都在str2中
$str1 = "abrabed defand"; $str2 = "abc"; $res = strspn($str1,$str2);; print_r($res);
//计算字符串的长度
$res = strlen("Shanghai"); print_r($res);
//去除字符串中的html标签
$res = strip_tags("Hello world!");
//查找相关函数
$res = strpbrk("I love Shanghai!","Sh");//查找字符串在另一字符串中的第一次出现位置到结束的部分 $res = strrchr("I love Shanghai! Sh eee","Sh");//查找字符串在另一字符串中的最后位置出现到结束的部分 $res = strstr("Hello world!","world");//查找字符串在另一字符串中的第一次出现到结束的部分,strchr()函数的别名 $res = stristr("Hello world!","world");//查找字符串在另一字符串中的第一次出现到结束的部分,不区分大小写 $res = strstr("Hello world!","world",true);//查找字符串在另一字符串中的第一次出现到开始的部分,strchr()函数的别名 $res = strcspn("Hello world!","w");//查找某个字符第一次出现前有多少个字符 $res = strpos("You love php, I love PHP too!","PHP");//一个字符串在另一字符串中第一次出现的位置,区分大小写 $res = stripos("You love php, I love PHP too!","PHP");//一个字符串在另一字符串中第一次出现的位置,不区分大小写 $res = strrpos("You love php, I love PHP too!","PHP");//返回字符串在另一字符串中最后出现的位置,区分大小写 $res = strripos("You love php, I love PHP too!","PHP");//返回字符串在另一字符串中最后出现的位置,不区分大小写 print_r($res);
//字符串比较函数
$res = strcmp("Hello world!","Hello world!");//比较两个字符串,区分大小写 $res = strnatcmp("Hello world!","Hello world!");//使用自然排序算法,比较两个字符串,区分大小写 $res = strcasecmp("shanghai","SHANGHAI");//比较两个字符串,不区分大小写 $res = strnatcasecmp("shanghai","SHANGHAI");//使用自然排序算法,比较两个字符串,不区分大小写 $res = strncmp("I love China!","I love Shanghai!",6);//比较两个字符串,区分大小写 只比较前面6个 $res = strncasecmp("I love China!","I love Shanghai!",6);//比较两个字符串,不区分大小写 只比较前面6个 $res = substr_compare("Hello world","Hello world",0);//从指定的开始位置比较两个字符串,相等返回0 print_r($res);
//返回字符串中单词的个数
$res = str_word_count("I love Shanghai!",0); $res = str_word_count("I love Shanghai!",1); $res = str_word_count("I love Shanghai!",2);//其中的键名是单词在字符串中的位置,键值是实际的单词 print_r($res);
//把字符串打散到数组中去
$res = str_split("Shanghai",2);//每两个一组 print_r($res);
//随机地打乱字符串中的所有字符
$res = str_shuffle("I love Shanghai"); print_r($res);
//把字符串重复指定的次数
$res = str_repeat("Shanghai",5); print_r($res);
//填充字符串到指定的长度
$str = "Hello World"; $res = str_pad($str,30,"."); print_r($res);
//替换字符串
$res = str_replace("WORLD","Shanghai","Hello world!");//替换字符串中的一些字符,对大小写敏感 $res = str_ireplace("WORLD","Shanghai","Hello world!");//替换字符串中的一些字符,对大小写不敏感 $res = substr_replace("Hello","world ",0,1);//把字符串的一部分替换为另一个字符串 print_r($res);
//把查询字符串解析到变量中
parse_str("name=Bill&age=60"); parse_str("name=Bill&age=60",$myArray);//解析到数组中 print_r($myArray);
//返回字符串中第一个字符的 ASCII 值
$res = ord("Shanghai"); print_r($res);
//去除字符串的左右字符
$str = "Hello World!"; $res = trim($str,"Hello");//从字符串左右侧移除字符 $res = ltrim($str,"Hello");//从字符串左侧移除字符 $res = rtrim($str,"Hello");//从字符串右侧移除字符 print_r($res);
//首字母大小写转换
$res = lcfirst("Hello world!");//把字符串的首字符转换为小写 $res = ucfirst("Hello world!");//把字符串的首字符转换为大写 print_r($res);
//把数组元素组合为字符串implode|join是一样的结果
$arr = array('Hello','World!','I','love','Shanghai!'); $res = implode(" ",$arr); $res = join(" ",$arr); print_r($res);
//html实体转换
$str='¤←\'\"?测试页面'; $res = htmlentities($str); //字符转换为 HTML 实体,对所有的html标记进行转换,html_entity_decode的反函数 print_r($res."\n",0); $res = html_entity_decode($res);//把 HTML 实体转换为字符,htmlentities的反函数 print_r($res."\n",0); $res = htmlspecialchars($str); //预定义的字符换为 HTML 实体,只对& " ' <>这几个基本的字符进行转码, htmlspecialchars_decode()的反函数 print_r($res."\n",0); $res = htmlspecialchars_decode($res);//预定义的html实体转回字符htmlspecialchars的反函数 print_r($res."\n");
//把十六进制值转换为 ASCII 字符
$res = hex2bin("481122656c6c6f20576f726c6421"); print_r($res);
//输出单个或者多个字符串
$str1 = "I love Shanghai!"; $str2="What a nice day!"; print($str1 . " " . $str2); $number1 = 9; $number2 = 123; $str = "Beijing"; //1、输出格式化的字符串 printf("测试参数1:%s测试参数2:%u ",$number1,$number2); //2、输出格式化的字符串,参数用数组传递 vprintf("测试参数1:%2\$s测试参数2:%1\$u ",array($number1,$number2)); //3、返回格式化的字符串 $res = sprintf("测试参数1:%s测试参数2:%u ",$number1,$number2); print_r($res,0); //4、返回格式化的字符串,参数用数组传递,%号的个数多余后面的参数需要使用占位符,如1\$表示第一个参数,2\$表示第二个参数 $res = vsprintf("测试参数1:%2\$s测试参数2:%1\$u ",array($number1,$number2)); print_r($res,0); $file = fopen("./runtime/test.txt","w"); //5、把格式化的字符串写入指定的文件,返回被写入字符串的长度 $res = fprintf($file,"测试参数1:%s测试参数2:%u",$number1,$number2); print_r($res,0); //6、把格式化的字符串写入指定的文件,参数用数组传递,返回被写入字符串的长度 $res = vfprintf($file,"测试参数1:%s测试参数2:%u",array($number1,$number2)); print_r($res);
//分隔字符串成数组
$str = "Hello world. I love Shanghai!"; $res = explode(" ",$str);//返回切割后的所有值 $res = explode(" ",$str,0);//返回一个元素的数组与explode(" ",$str,1)一样 $res = explode(" ",$str,2);//切割成几个元素 $res = explode(" ",$str,-2);//切割后的数组后面几个值不要 print_r($res);
//单向的字符串加密法
$res = crypt("Hello world!"); print_r($res); /** 第二个参数 * 0 - 数组,ASCII 值为键名,出现的次数为键值 1 - 数组,ASCII 值为键名,出现的次数为键值,只列出出现次数大于 0 的值 2 - 数组,ASCII 值为键名,出现的次数为键值,只列出出现次数等于 0 的值 3 - 字符串,带有所有使用过的不同的字符 4 - 字符串,带有所有未使用过的不同的字符 */ $res = count_chars("Hello World!",3);//查看字符串中使用了哪些字符 $strArray = count_chars("Hello World!",1);//只列出出现次数大于 0 foreach ($strArray as $key=>$value) { print_r("字符 被找到 $value 次。 ",0); } exit; $res = convert_uuencode("Hello world!");//使用 uuencode 算法对字符串进行编码 print_r($res,0); $res = convert_uudecode($res);//解码 uuencode 编码字符串 print_r($res);
//每隔多少个字符后面插入指定的字符串
$str = "Hello world!"; $res = chunk_split($str,6,"..."); print_r($res);
//删除字符串右侧的空白字符或其他字符
$res = chop("Hello World!","World!"); print_r($res);
//ASCII 值返回字符
echo chr(61) . ""; // 十进制 echo chr(061) . ""; // 八进制值 echo chr(0x61) . ""; // 十六进制值 exit;
//字符串转换为十六进制值
$res = bin2hex("01"); print_r($res);
//在指定的字符前添加反斜杠
$res = addcslashes("Shanghai is the 'biggest' city in China. 'a'",'a'); $res = stripcslashes($res);//删除addcslashes函数添加的\ print_r($res);
//在预定义的字符前添加反斜杠(预定义字符串包括:单引号、双引号、反斜杠、null)
//php5.4之前的版本可以调用get_magic_quotes_gpc()检查是否默认开启了将POST、GET、COOKIE中的值自动转移的功能,如果开启了的就不需要调用这个函数了,避免重复转移,php5.4之后的版本需要自己去转移
$res = addslashes("Shanghai is the 'biggest' city in China. 'a'"); $res = stripslashes($res);//删除addslashes函数添加的\ print_r($res);