搜索
首页后端开发php教程php 常用字符串函数总结_PHP教程

php 常用字符串函数总结_PHP教程

Jul 21, 2016 pm 03:53 PM
ltrimphprtrimtrim函数别名字符串常用总结格式化输出

1.格式化输出

chop 是rtrim()的别名;

ltrim()

trim()

nl2br()将\n转换成


print,echo,printf(),sprintf():

echo()不是函数,print()是函数,有返回值,boolen,false,true;

printf()格式化输出

--函数,把文字格式化以后输出,直接调用系统调用进行IO的,他是非缓冲的。如:
$name="hunte";
$age=25;
printf("my name is %s, age %d", $name, $age);

sprintf()格式化字符串,然后赋给一个变量,但是不输出,类似于c了

echo nl2br("foo isn't\n bar");
echo "foo isn't\n bar";
?>

--跟printf相似,但不打印,而是返回格式化后的文字,其他的与printf一样。如:
char sql[256];
sprintf(sql,"select * from table where no = '%s'",bankno);
它的功能只是把""里面的语句赋给了变量sql。

strtolower

strtoupper

ucwords

ucfirst

2.字符串的连接和分割

(1)array explode(string input ,string separator , int limit)

使用一个字符串分割另一个字符串

// 示例 1
$pizza  = "piece1 piece2 piece3 piece4 piece5 piece6";
$pieces = explode(" ", $pizza);
echo $pieces[0]; // piece1
echo $pieces[1]; // piece2

// 示例 2
$data = "foo:*:1023:1000::/home/foo:/bin/sh";
list($user, $pass, $uid, $gid, $gecos, $home, $shell) = explode(":", $data);
echo $user; // foo
echo $pass; // *

?>  


例子 2. limit 参数示例

$str = 'one|two|three|four';

// 正数的 limit
print_r(explode('|', $str, 2));

// 负数的 limit
print_r(explode('|', $str, -1));
?>  



string strtok( string input ,string separator)

$string = "This is\tan example\nstring";
/* Use tab and newline as tokenizing characters as well          */
$tok = strtok($string, " \n\t");

//拿着空格,\n,\t作为令牌分割字符串

while ($tok !== false) {
            echo "Word=$tok
";
            $tok = strtok(" \n\t");
}
?>

结果:

Word=This
Word=is
Word=an
Word=example
Word=string

(2.)字符串的截取

$test="Your customer service is excellent";
echo substr($test,1);////////our customer service is excellent
echo "
";
echo substr($test,-9);//////从末尾起长度是9excellent
echo "
";
echo substr($test,0,4);////从0位置开始长度是4Your
echo "
";
echo substr($test,5,-13);/从第四个开始到倒数第13个字符customer service
echo "
";

$test="Your customer service is excellent";
echo substr($test,1);
echo "
";
echo substr($test,-11);
echo "
";
echo substr($test,0,6);
echo "
";
echo substr($test,5,-13);
echo "
";

our customer service is excellent
s excellent
Your c
customer service

(3)join()字符串的链接

3.字符串的查找


(1)string strstr ( string haystack, string needle ) 别名:strchr,stristr和strstr类似不同在于不区分大小写

strrchr()相反的,查找的是最后一次出现的字符串

第一次出现起的字符串
$email = 'user@example.com';
$domain = strstr($email, '@');
echo $domain; // prints @example.com
?> 
$email = 'user@example.com';
$domain =strstr($email,'e');
$domain2 =strrchr($email,'e');//最后一次出现起的字符串
echo $domain; 
echo "
";
echo $domain2;

er@example.com
e.com

(2)查找位置

int strpos(string str,string needle,[int offset]) 没有找到返回的是false

返回从offset开始在str中查找needle的位置

$eg:$t-'hello world';

       echo strpos($t,'o',5);

     //7 从第o开始,查找o这个变量的位置,结果为7

int strrpos()

5.替换

str_replace("%body%","blank","







6 。大写小问题


Strpos
查找字符串中第一次出现的字符串的位置

Strrpos
查找字符串中某字符,继第一次之后的最先出现的位置。

strpos(stripos无大小写)
strrpos(strripos无大小写)






strstr
stristr(无大小写)

str_replace 
str_ireplace(无大小写)

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/318672.htmlTechArticle1.格式化输出 chop是rtrim()的别名; ltrim() trim() nl2br()将\n转换成br print,echo,printf(),sprintf(): echo()不是函数,print()是函数,有返回值,boolen,...
声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
使用PHP发送电子邮件的最佳方法是什么?使用PHP发送电子邮件的最佳方法是什么?May 08, 2025 am 12:21 AM

ThebestapproachforsendingemailsinPHPisusingthePHPMailerlibraryduetoitsreliability,featurerichness,andeaseofuse.PHPMailersupportsSMTP,providesdetailederrorhandling,allowssendingHTMLandplaintextemails,supportsattachments,andenhancessecurity.Foroptimalu

PHP中依赖注入的最佳实践PHP中依赖注入的最佳实践May 08, 2025 am 12:21 AM

使用依赖注入(DI)的原因是它促进了代码的松耦合、可测试性和可维护性。1)使用构造函数注入依赖,2)避免使用服务定位器,3)利用依赖注入容器管理依赖,4)通过注入依赖提高测试性,5)避免过度注入依赖,6)考虑DI对性能的影响。

PHP性能调整技巧和技巧PHP性能调整技巧和技巧May 08, 2025 am 12:20 AM

phperformancetuningiscialbecapeitenhancesspeedandeffice,whatevitalforwebapplications.1)cachingwithapcureduccureducesdatabaseloadprovesrovesponsemetimes.2)优化

PHP电子邮件安全性:发送电子邮件的最佳实践PHP电子邮件安全性:发送电子邮件的最佳实践May 08, 2025 am 12:16 AM

ThebestpracticesforsendingemailssecurelyinPHPinclude:1)UsingsecureconfigurationswithSMTPandSTARTTLSencryption,2)Validatingandsanitizinginputstopreventinjectionattacks,3)EncryptingsensitivedatawithinemailsusingOpenSSL,4)Properlyhandlingemailheaderstoa

您如何优化PHP应用程序的性能?您如何优化PHP应用程序的性能?May 08, 2025 am 12:08 AM

TOOPTIMIZEPHPAPPLICITIONSFORPERSTORANCE,USECACHING,数据库imization,opcodecaching和SererverConfiguration.1)InlumentCachingWithApcutCutoredSatfetchTimes.2)优化的atabasesbasesebasesebasesbasesbasesbaysbysbyIndexing,BeallancingAndWriteExing

PHP中的依赖注入是什么?PHP中的依赖注入是什么?May 07, 2025 pm 03:09 PM

依赖性注射inphpisadesignpatternthatenhancesFlexibility,可检验性和ManiaginabilybyByByByByByExternalDependencEctenceScoupling.itallowsforloosecoupling,EasiererTestingThroughMocking,andModularDesign,andModularDesign,butquirscarecarefulscarefullsstructoringDovairing voavoidOverOver-Inje

最佳PHP性能优化技术最佳PHP性能优化技术May 07, 2025 pm 03:05 PM

PHP性能优化可以通过以下步骤实现:1)在脚本顶部使用require_once或include_once减少文件加载次数;2)使用预处理语句和批处理减少数据库查询次数;3)配置OPcache进行opcode缓存;4)启用并配置PHP-FPM优化进程管理;5)使用CDN分发静态资源;6)使用Xdebug或Blackfire进行代码性能分析;7)选择高效的数据结构如数组;8)编写模块化代码以优化执行。

PHP性能优化:使用OpCode缓存PHP性能优化:使用OpCode缓存May 07, 2025 pm 02:49 PM

opcodecachingsimplovesphperforvesphpermance bycachingCompiledCode,reducingServerLoadAndResponSetimes.1)itstorescompiledphpcodeinmemory,bypassingparsingparsingparsingandcompiling.2)useopcachebachebachebachebachebachebachebysettingparametersinphametersinphp.ini,likeememeryconmorysmorysmeryplement.33)

See all articles

热AI工具

Undresser.AI Undress

Undresser.AI Undress

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

AI Clothes Remover

AI Clothes Remover

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

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

Video Face Swap

Video Face Swap

使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热工具

DVWA

DVWA

Damn Vulnerable Web App (DVWA) 是一个PHP/MySQL的Web应用程序,非常容易受到攻击。它的主要目标是成为安全专业人员在合法环境中测试自己的技能和工具的辅助工具,帮助Web开发人员更好地理解保护Web应用程序的过程,并帮助教师/学生在课堂环境中教授/学习Web应用程序安全。DVWA的目标是通过简单直接的界面练习一些最常见的Web漏洞,难度各不相同。请注意,该软件中

mPDF

mPDF

mPDF是一个PHP库,可以从UTF-8编码的HTML生成PDF文件。原作者Ian Back编写mPDF以从他的网站上“即时”输出PDF文件,并处理不同的语言。与原始脚本如HTML2FPDF相比,它的速度较慢,并且在使用Unicode字体时生成的文件较大,但支持CSS样式等,并进行了大量增强。支持几乎所有语言,包括RTL(阿拉伯语和希伯来语)和CJK(中日韩)。支持嵌套的块级元素(如P、DIV),

WebStorm Mac版

WebStorm Mac版

好用的JavaScript开发工具

VSCode Windows 64位 下载

VSCode Windows 64位 下载

微软推出的免费、功能强大的一款IDE编辑器

SublimeText3汉化版

SublimeText3汉化版

中文版,非常好用