php常用字符串函数小结
php内置了98个字符串函数(除了基于正则表达式的函数,正则表达式在此不在讨论范围),能够处理字符串中能遇到的每一个方面内容,本文对常用字符串函数进行简单的小结,主要包含以下8部分:1.确定字符串长度、2.比较字符串、3.分割连接反转、4.html与字符串相互转化、5.填充和剔除字符串、6.统计字符和单词个数、7.查找替换截取、8.大小写处理。
确定字符串长度
strlen函数和mb_strlen函数,后者需要开启mbstring扩展
<?php
header('content-type:text/html;charset=utf-8');
$str = 'abcdef';
echo strlen($str); // 6
echo "<br/>";
$str = ' ab cd ';
echo mb_strlen($str); // 7
echo "<br/>";
//strlen 是计算字符串"字节"长度
//mb_strlen,是根据编码,计算字符串的"字符"个数.
$str='中华人民共和国';
echo "字节长度是".strlen($str);//在 UTF-8编码下,一个汉字占3个字节 在gbk中一个汉字占2个字节
echo "<br/>";
echo "字符长度是".mb_strlen($str,'utf-8');
?>
比较字符串
strcmp函数、strcasecmp函数、strspn函数、strcspn函数
<?php
$pwd="userpwd";
$pwd2="Userpwd";
//区分大小写
if (strcmp($pwd, $pwd2) !=0) {
echo "password do not match";
} else{
echo "password match";
}
$email1="www.baidu.com";
$email2="WWW.BAIDU.COM";
//不区分大小写
if (!strcasecmp($email1, $email2)) {
echo "ok",'<br>';
}
//求两个字符串相同的部分
$password="1233345";
if (strspn($password,"1234567890")==strlen($password)) {
echo "the password connot consist solely of numbers";
}
//
$password="a12345";
if (strcspn($password, "1234567890")==0) {
echo "the password connot consist solely of numbers";
}
?>
分割连接反转
str_split函数、split函数、explode函数和implode函数
<?php
header('content-type:text/html;charset=utf-8');
$str = "Hello Friend";
$arr1 = str_split($str);
print_r($arr1);
$arr2 = str_split($str, 3);
print_r($arr2);
$str = 'abc,中国,美国,日本';
// explode,是根据指定的分割符,把字符串拆成数组.
$arr = explode(',',$str);
print_r($arr);
// implode,是根据指定的连接符,把数组再拼接成字符串
$arr = explode(',',$str);
echo implode('~',$arr),'<br />';
// 你可以只传一个数组做参数,不指定连接符,
// 这样,将把数组单元直接拼接起来
echo implode($arr);
?>
html与字符串相互转化
htmlspecialchars函数、strip_tags函数、get_html_translation_table函数和addcslashes函数和htmlentities函数
<?php
$str = "hello ', world";
echo $str,'<br />';
echo $str= addslashes($str),'<br />';
echo stripslashes($str),'<br />';
$str = '<ab>';
echo $str,'<br />';
echo htmlspecialchars($str);
echo "</br>";
$str="Email <a href='admin@qq.com'>example@qq.com</a>";
echo strip_tags($str);
?>
填充和剔除字符串
trim函数、ltrim函数、rtrim函数、str_pad函数、chunk_split函数
<?php
$str = '12345678';
echo chunk_split($str,3,',');
echo "<br>";
$text = "\t\tThese are a few words :) ... ";
echo trim($text);
echo "<br>";
echo ltrim($text,'\t'),'<br>';
echo rtrim($text,'\r'),'<br>';
echo str_pad('apple', 6)."is good.";
?>
统计字符和单词个数
count_chars函数和str_word_count
<?php
$data = "Two Ts and one F.";
foreach (count_chars($data, 1) as $i => $val) {
echo "There were $val instance(s) of \"" , chr($i) , "\" in the string.\n";
}
echo "<hr>";
$str = "Hello fri3nd, you're looking good today!";
print_r(str_word_count($str, 1));
?>
查找替换截取
strpos函数、str_replace函数、substr_replace函数、substr函数、strstr函数
<?php
$substr = "index.html";
$log = <<< logfile
192.168.1.11:/www/htdocs/index.html:[2016/08/10:21:58:27]
192.168.1.11:/www/htdocs/index.html:[2016/08/18:01:51:37]
192.168.1.11:/www/htdocs/index.html:[2016/08/20:11:48:27]
logfile;
$pos =strpos($log, $substr);
$pos2=strpos($log,"\n",$pos);
$pos=$pos+strlen($substr)+1;
$timestamp=substr($log,$pos,$pos2-$pos);
echo "The file $substr was first accessed on:$timestamp";
echo "<br>";
$author="lester@example.com";
$author=str_replace("@", "at", $author);
echo "connect the author of this article at $author";
echo "<br>";
echo ltrim(strstr($author,"@"), "@");
?>
大小写处理
strtolower函数、strtoupper函数、ucfirst函数、ucwords函数
<?php
$url="http://WWWW.BAIDU.COM";
echo strtolower($url),'<br>';
$str="hello world";
echo strtoupper($str),'<br>';
$str="php is the most popular language ";
echo ucfirst($str),'<br>';
echo ucwords($str);
?>
php常用字符串函数小结的更多相关文章
- [转]MySQL常用Json函数和MySQL常用字符串函数
MySQL常用Json函数:https://www.cnblogs.com/waterystone/p/5626098.html MySQL常用字符串函数:https://www.cnblogs.co ...
- js进阶js中支持正则的四个常用字符串函数(search march replace split)
js进阶js中支持正则的四个常用字符串函数(search march replace split) 一.总结 代码中详细四个函数的用法 search march replace split 二.js进 ...
- Delphi常用字符串函数
Delphi常用字符串函数 一.字符转换函数1.ord(input[i])返回字符表达式 input 左端起第 I 字符的ASCII 码值.2.CHAR()将ASCII 码转换为字符.如果没有输入 ...
- MySQL最常用字符串函数
字符串函数 是最常用的的一种函数,在一个具体应用中通常会综合几个甚至几类函数来实现相应的应用: 1.LOWER(column|str):将字符串参数值转换为全小写字母后返回 mysql> sel ...
- MySQL常用字符串函数
字符串函数 是最常用的的一种函数,在一个具体应用中通常会综合几个甚至几类函数来实现相应的应用: 1.LOWER(column|str):将字符串参数值转换为全小写字母后返回 mysql> sel ...
- javascript常用字符串函数和本地存储
concat将两个或多个字符的文本组合起来,返回一个新的字符串.var a = "hello";var b = ",world";var c = a.conca ...
- php 常用字符串函数总结
php里面自带的字符串函数,日期函数,数组函数等,有时候可以帮助我们解决很复杂的问题,运用起来也比较简单. 下面总结了一下常用的字符串函数. addcslashes — 为字符串里面的部分字符添加反斜 ...
- C语言常用字符串函数
string.h头文件中常用的函数 C 库函数 - strcat() char *strcat(char *dest, const char *src) 把 src 所指向的字符串追加到 dest 所 ...
- PHP 常用字符串函数整理
PHP语言中的字符串函数也是一个比较易懂的知识.今天我们就为大家总结了将近12种PHP字符串函数,希望对又需要的朋友有所帮助,增加读者朋友的PHP知识库. 1.查找字符位置函数 strpos($str ...
随机推荐
- css3【语法要点】
语法要点 display: -webkit-box; /* 老版本语法: Safari, iOS, Android browser, older WebKit browsers. */ display ...
- MongoDB学习笔记~客户端命令行的使用
回到目录 当我们从MongoDB网站下载安装包之后,它会伴随有一系列的工具,服务器程序mongod是我们耳熟能详的了,客户端mongo和性能检测mongostat我们可能就没有用过了,今天主要是介绍一 ...
- kmeans算法c语言实现,能对不同维度的数据进行聚类
最近在苦于思考kmeans算法的MPI并行化,花了两天的时间把该算法看懂和实现了串行版. 聚类问题就是给定一个元素集合V,其中每个元素具有d个可观察属性,使用某种算法将V划分成k个子集,要求每个子集内 ...
- shell 判断语句
1.字符串判断 str1 = str2 当两个串有相同内容.长度时为真str1 != str2 当串str1和str2不等时为真-n str1 当串的长度大于0时为真(串非空)-z str1 当串的长 ...
- Java Generics and Collections-2.2
2.2 Wildcards with extends 前面介绍过List<Integer>不是List<Number>的子类,即前者不能替换后者, java使用? extend ...
- c++适配器
容器适配器是是标准库中通用的概念,包括容器适配器.迭代器适配器和函数适配器,本质上,适配器是使一种事物的行为类似于另一种事物的的行为的一种机制,容器适配器使一种已经存在的容器类型采用另一种不同的抽象类 ...
- 我的CS考研路
说在前面 从去年7月15号正式准备考研以来,直到今天,3月19号,一共经历8个多月,考研初步告捷,在此想跟大家分享一下自己的经验,希望能对接下来考研的学弟学妹们有所帮助. 首先介绍下我自己的情况,本科 ...
- uva 140 bandwidth (好题) ——yhx
Bandwidth Given a graph (V,E) where V is a set of nodes and E is a set of arcs in VxV, and an orde ...
- UVA 11404 Palindromic Subsequence[DP LCS 打印]
UVA - 11404 Palindromic Subsequence 题意:一个字符串,删去0个或多个字符,输出字典序最小且最长的回文字符串 不要求路径区间DP都可以做 然而要字典序最小 倒过来求L ...
- Hibernate入门
脏检查及刷新缓存机制: 脏检查:当事物提交时,Hibernate会对session中持久状态的对象进行检测, 判断对象的数据是否发生改变 为什么要进行脏检查? 如果对象发生了改变,就需要将改变更新到数 ...