当前位置:网站首页>PHP 零基础入门笔记(11):字符串 String
PHP 零基础入门笔记(11):字符串 String
2022-04-22 20:09:00 【游戏编程】
字符串 String
字符串类型
- 单引号字符串
双引号字符串
nowdoc 字符串
heredoc 字符串 示例
<?php// 1、单引号字符串$str1 = 'hello world';var_dump($str1);// string(11) "hello world"// 2、双引号字符串$str2 = "hello world";var_dump($str2);// string(11) "hello world"// 3、nowdoc$str2 = <<<'EOD'hello worldEOD;var_dump($str2);// string(11) "hello world"// 4、heredoc$str1 = <<<EODhello worldEOD;var_dump($str1);// string(11) "hello world"
字符串转义
系统会特殊处理:反斜杠 + 字母
例如:
\r\n 回车换行
PHP 中常用的转义字符
\' 在单引号字符串中显示单引号\" 在双引号字符串中显示双引号\r 回车(回到当行的行首)\n 换行\t tab\$ $在PHP中作为变量符号
单引号和双引号的区别
1、单引号中能够识别 \' , 而双引号中不能识别 \'
<?php$str1 = 'hello \'world';$str2 = "hello \'world";var_dump($str1);// string(12) "hello 'world"var_dump($str2);// string(13) "hello \'world"
2、单引号中不能识别变量,而双引号中能识别 $ ,解析变量
- 变量本省需要与后面内容有区分,保证变量独立性
使用变量标识符区分,加一组大括号
{}
<?php$a = 'Tom';$str1 = 'hello $a world';var_dump($str1);// string(14) "hello $a world"$str2 = "hello $a world";var_dump($str2);// string(15) "hello Tom world"$str3 = "hello $aworld";var_dump($str3);// PHP Notice: Undefined variable: aworld$str4 = "hello {$a}world";var_dump($str4);// string(14) "hello Tomworld"
结构化定义字符串变量的规则
结构化定义字符串对应的边界符有条件
- 上边界符后面不能有任何内容
- 下边界符必须最左边顶格
- 下边界符后面只能跟符号,不能跟任何字符
- 结构化定义字符串的内部(边界符之间)的所有内容都是字符串本身
<?php$str1 = <<<EODhello worldEOD;var_dump($str1);// string(11) "hello world"
字符串长度
strlen(string $string): int
示例
<?php$str1 = 'hello world';$str2 = '你好世界'; // 中文在utf8字符集下占3个字节var_dump(strlen($str1)); // int(11)var_dump(strlen($str2)); // int(12)
多字节字符串扩展模块 mbstring(Multi String)
<?php$str1 = 'hello world';$str2 = '你好世界';var_dump(mb_strlen($str1)); // int(11)var_dump(mb_strlen($str2)); // int(4)
字符串相关函数
1、转换函数
implode — 将一个一维数组的值转化为字符串
implode(string $glue, array $pieces): stringimplode(array $pieces): stringprint_r(implode('|', ['北京', '上海', '广州']));// 北京|上海|广州
explode — 使用一个字符串分割另一个字符串
explode(string $separator, string $string, int $limit = PHP_INT_MAX): arrayprint_r(explode('|', '北京|上海|广州'));// Array// (// [0] => 北京// [1] => 上海// [2] => 广州// )
str_split — 将字符串转换为数组
str_split(string $string, int $split_length = 1): array// 注意:中文字符拆分有问题print_r(str_split('helloworld', 5));// Array// (// [0] => hello// [1] => world// )
2、截取函数
trim — 去除字符串首尾处的空白字符(或者其他字符)
trim(string $str, string $character_mask = " \t\n\r\0\x0B"): string
ltrim — 删除字符串开头的空白字符(或其他字符)
ltrim(string $str, string $character_mask = ?): string
rtrim — 删除字符串末端的空白字符(或者其他字符)
rtrim(string $str, string $character_mask = ?): string
示例
<?phpvar_dump(trim(' 你好,世界 '));// string(15) "你好,世界"var_dump(ltrim(' 你好,世界 '));// string(16) "你好,世界 "var_dump(rtrim(' 你好,世界 '));// string(16) " 你好,世界"
substr — 返回字符串的子串
substr(string $string, int $offset, ?int $length = null): string// 注意:中文字符截取有问题var_dump(substr('Hello World', 6, 5));// string(5) "World"
strstr — 返回 haystack 字符串从 needle 第一次出现的位置开始到结尾的字符串(取文件后缀名)
strstr(string $haystack, mixed $needle, bool $before_needle = false): stringvar_dump(strstr('Hello,World', 'W'));// string(5) "World"
3、大小写转换
strtolower — 将字符串转化为小写
strtolower(string $string): string
strtoupper — 将字符串转化为大写
strtoupper(string $string): string
ucfirst — 将字符串的首字母转换为大写
ucfirst(string $str): string
示例
var_dump(strtolower('Hello, World'));// string(12) "hello, world"var_dump(strtoupper('Hello, World'));// string(12) "HELLO, WORLD"var_dump(ucfirst('hello, world'));// string(12) "Hello, world"
4、查找函数
strpos — 返回 needle 在 haystack 中首次出现的数字位置。
strpos(string $haystack, mixed $needle, int $offset = 0): intvar_dump(strpos('Hello, World', 'o'));// int(4)
strrpos — 返回字符串 haystack 中 needle 最后一次出现的数字位置。
strrpos(string $haystack, string $needle, int $offset = 0): intvar_dump(strrpos('Hello, World', 'o'));// int(8)
5、替换函数
str_replace — 子字符串替换, 该字符串或数组是将 subject 中全部的 search 都被 replace 替换之后的结果。
str_replace( mixed $search, mixed $replace, mixed $subject, int &$count = ?): mixedvar_dump(str_replace('o', 'A', 'Hello, World'));// string(12) "HellA, WArld"
6、格式化函数
printf — 输出格式化字符串
printf(string $format, mixed $args = ?, mixed $... = ?): int
sprintf — 返回格式化字符串
sprintf(string $format, mixed ...$values): string
格式化参数:https://www.php.net/manual/zh/function.sprintf.php
// 直接输出,返回数字$ret1 = printf('my name is %s, and year old is %d', 'Tom', 18);// my name is Tom, and year old is 18var_dump($ret1); // int(34)// 返回字符串$ret2 = sprintf('my name is %s, and year old is %d', 'Tom', 18);var_dump($ret2);// string(34) "my name is Tom, and year old is 18"
7、其他
str_repeat — 重复一个字符串
str_repeat(string $input, int $multiplier): stringecho str_repeat('ABC', 5);// ABCABCABCABCABC
str_shuffle — 随机打乱一个字符串
str_shuffle(string $str): stringecho str_shuffle('ABCDEFG');// GBEACDF
作者:彭世瑜
游戏编程 ️,一个游戏开发收藏夹~
如果图片长时间未显示,请使用Chrome内核浏览器。
版权声明
本文为[游戏编程]所创,转载请带上原文链接,感谢
https://www.233tw.com/php/118715
边栏推荐
- [basic knowledge of automated testing] basic concepts and common frameworks of automated testing
- Is it safe to open futures account on mobile phone? Do you need the assistance of offline account manager?
- go实现 银行卡Luhn校验
- 微服务高性能高可用架构设计
- 微服务架构从0到1,搭建全过程记录,手把手教学,连载中...
- What is the reason why easycvr streaming media kernel cannot be started?
- Why puddingswap may be a strong dark horse in the field of gamefi?
- 【Unity】可玩广告Luna Playable插件的踩坑记录
- 金仓数据库KingbaseES的连接方法
- 使用rpmbuild打包php
猜你喜欢

Anaconda新建环境并安装GPU版本的pytorch

An error occurs when starting Kingbase stand-alone service: invalid value for parmeter "bindpuplist": "0-95"

Embedded Web project (I) -- introduction of web server

Why is x16 slower than X8?

envoy代理后获取客户端真实IP

文件上传问题记录

sys_ctl启动kingbase单机服务时报错:could not bind IPv4 address “0.0.0.0“: Address already in use

看大网站如何玩转网站加速(CDN)的秘密

Format for creating Zimbra LDAP users using LDAP clients

sys_ When CTL starts Kingbase stand-alone service, an error is reported: could not bind IPv4 address "0.0.0.0": address already in use
随机推荐
金仓数据库KingbaseES服务启动方法
Security features of kingbasees
Industry trends are far more important than efforts -- top test technology summary
What is the reason why the camera device with built-in 4G card of Haikang cannot register with easycvr platform?
<山东大学项目实训>辐射预计算渲染及后处理降噪系统研究(一)
Is it better to do operation or software testing?
Hard work alone is not enough! And this
396. 旋转函数(数学规律 + 迭代法)
Method of database startup installer in kingbasees
行业趋势远比努力更重要--顶测科技总结
calico官网网络拓扑实现:基于eNSP与VMVare
使用rpmbuild打包php
做运营好,还是做软件测试好?
Pytorch deep learning practice 08 loading data set
图像预训练模型的起源解说和使用示例
[Chongqing Guangdong education] reference materials of management principles of Sichuan Agricultural University
[program source code] graduation project - second-hand trading website
Network tunneling technology
运动戴什么耳机好、跑步听音乐的最佳设备
期货开户在手机上办理安全吗?需要线下客户经理协助办理吗?