本篇文章主要介绍php中常用字符串函数,感兴趣的朋友参考下,希望对大家有所帮助。
阅读目录
确定字符串长度
比较字符串
分割连接反转
html与字符串相互转化
填充和剔除字符串
统计字符和单词个数
查找替换截取
大小写处理
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中常用字符串函数的详细内容。更多信息请关注PHP中文网其他相关文章!

PHP主要是过程式编程,但也支持面向对象编程(OOP);Python支持多种范式,包括OOP、函数式和过程式编程。PHP适合web开发,Python适用于多种应用,如数据分析和机器学习。

PHP起源于1994年,由RasmusLerdorf开发,最初用于跟踪网站访问者,逐渐演变为服务器端脚本语言,广泛应用于网页开发。Python由GuidovanRossum于1980年代末开发,1991年首次发布,强调代码可读性和简洁性,适用于科学计算、数据分析等领域。

PHP适合网页开发和快速原型开发,Python适用于数据科学和机器学习。1.PHP用于动态网页开发,语法简单,适合快速开发。2.Python语法简洁,适用于多领域,库生态系统强大。

PHP在现代化进程中仍然重要,因为它支持大量网站和应用,并通过框架适应开发需求。1.PHP7提升了性能并引入了新功能。2.现代框架如Laravel、Symfony和CodeIgniter简化开发,提高代码质量。3.性能优化和最佳实践进一步提升应用效率。

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

PHP类型提示提升代码质量和可读性。1)标量类型提示:自PHP7.0起,允许在函数参数中指定基本数据类型,如int、float等。2)返回类型提示:确保函数返回值类型的一致性。3)联合类型提示:自PHP8.0起,允许在函数参数或返回值中指定多个类型。4)可空类型提示:允许包含null值,处理可能返回空值的函数。

PHP中使用clone关键字创建对象副本,并通过\_\_clone魔法方法定制克隆行为。1.使用clone关键字进行浅拷贝,克隆对象的属性但不克隆对象属性内的对象。2.通过\_\_clone方法可以深拷贝嵌套对象,避免浅拷贝问题。3.注意避免克隆中的循环引用和性能问题,优化克隆操作以提高效率。

PHP适用于Web开发和内容管理系统,Python适合数据科学、机器学习和自动化脚本。1.PHP在构建快速、可扩展的网站和应用程序方面表现出色,常用于WordPress等CMS。2.Python在数据科学和机器学习领域表现卓越,拥有丰富的库如NumPy和TensorFlow。


热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

AI Hentai Generator
免费生成ai无尽的。

热门文章

热工具

记事本++7.3.1
好用且免费的代码编辑器

ZendStudio 13.5.1 Mac
功能强大的PHP集成开发环境

VSCode Windows 64位 下载
微软推出的免费、功能强大的一款IDE编辑器

螳螂BT
Mantis是一个易于部署的基于Web的缺陷跟踪工具,用于帮助产品缺陷跟踪。它需要PHP、MySQL和一个Web服务器。请查看我们的演示和托管服务。

SecLists
SecLists是最终安全测试人员的伙伴。它是一个包含各种类型列表的集合,这些列表在安全评估过程中经常使用,都在一个地方。SecLists通过方便地提供安全测试人员可能需要的所有列表,帮助提高安全测试的效率和生产力。列表类型包括用户名、密码、URL、模糊测试有效载荷、敏感数据模式、Web shell等等。测试人员只需将此存储库拉到新的测试机上,他就可以访问到所需的每种类型的列表。