Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
檀越@新空间
Coding Tree
提交
ece1651d
C
Coding Tree
项目概览
檀越@新空间
/
Coding Tree
通知
2
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
C
Coding Tree
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
提交
ece1651d
编写于
3月 08, 2022
作者:
彭世瑜
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
fix
上级
ffa330cc
变更
2
显示空白变更内容
内联
并排
Showing
2 changed file
with
221 addition
and
2 deletion
+221
-2
blog/php-basic/string.md
blog/php-basic/string.md
+217
-2
doc/miniprogram.md
doc/miniprogram.md
+4
-0
未找到文件。
blog/php-basic/string.md
浏览文件 @
ece1651d
...
@@ -119,7 +119,6 @@ var_dump($str4);
...
@@ -119,7 +119,6 @@ var_dump($str4);
-
下边界符后面只能跟符号,不能跟任何字符
-
下边界符后面只能跟符号,不能跟任何字符
2.
结构化定义字符串的内部(边界符之间)的所有内容都是字符串本身
2.
结构化定义字符串的内部(边界符之间)的所有内容都是字符串本身
```
php
```
php
<?php
<?php
$str1
=
<<<EOD
$str1
=
<<<EOD
...
@@ -162,4 +161,220 @@ var_dump(mb_strlen($str2)); // int(4)
...
@@ -162,4 +161,220 @@ var_dump(mb_strlen($str2)); // int(4)
```
```
https://www.bilibili.com/video/BV18x411H7qD?p=79&spm_id_from=pageDriver
## 字符串相关函数
1、转换函数
implode — 将一个一维数组的值转化为字符串
```
php
implode
(
string
$glue
,
array
$pieces
)
:
string
implode
(
array
$pieces
)
:
string
print_r
(
implode
(
'|'
,
[
'北京'
,
'上海'
,
'广州'
]));
// 北京|上海|广州
```
explode — 使用一个字符串分割另一个字符串
```
php
explode
(
string
$separator
,
string
$string
,
int
$limit
=
PHP_INT_MAX
)
:
array
print_r
(
explode
(
'|'
,
'北京|上海|广州'
));
// Array
// (
// [0] => 北京
// [1] => 上海
// [2] => 广州
// )
```
str_split — 将字符串转换为数组
```
php
str_split
(
string
$string
,
int
$split_length
=
1
)
:
array
// 注意:中文字符拆分有问题
print_r
(
str_split
(
'helloworld'
,
5
));
// Array
// (
// [0] => hello
// [1] => world
// )
```
2、截取函数
trim — 去除字符串首尾处的空白字符(或者其他字符)
```
php
trim
(
string
$str
,
string
$character_mask
=
"
\t\n\r\0\x0B
"
)
:
string
```
ltrim — 删除字符串开头的空白字符(或其他字符)
```
php
ltrim
(
string
$str
,
string
$character_mask
=
?
)
:
string
```
rtrim — 删除字符串末端的空白字符(或者其他字符)
```
php
rtrim
(
string
$str
,
string
$character_mask
=
?
)
:
string
```
示例
```
php
<?php
var_dump
(
trim
(
' 你好,世界 '
));
// string(15) "你好,世界"
var_dump
(
ltrim
(
' 你好,世界 '
));
// string(16) "你好,世界 "
var_dump
(
rtrim
(
' 你好,世界 '
));
// string(16) " 你好,世界"
```
substr — 返回字符串的子串
```
php
substr
(
string
$string
,
int
$offset
,
?
int
$length
=
null
)
:
string
// 注意:中文字符截取有问题
var_dump
(
substr
(
'Hello World'
,
6
,
5
));
// string(5) "World"
```
strstr — 返回 haystack 字符串从 needle 第一次出现的位置开始到结尾的字符串(取文件后缀名)
```
php
strstr
(
string
$haystack
,
mixed
$needle
,
bool
$before_needle
=
false
)
:
string
var_dump
(
strstr
(
'Hello,World'
,
'W'
));
// string(5) "World"
```
3、大小写转换
strtolower — 将字符串转化为小写
```
php
strtolower
(
string
$string
)
:
string
```
strtoupper — 将字符串转化为大写
```
php
strtoupper
(
string
$string
)
:
string
```
ucfirst — 将字符串的首字母转换为大写
```
php
ucfirst
(
string
$str
)
:
string
```
示例
```
php
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 中首次出现的数字位置。
```
php
strpos
(
string
$haystack
,
mixed
$needle
,
int
$offset
=
0
)
:
int
var_dump
(
strpos
(
'Hello, World'
,
'o'
));
// int(4)
```
strrpos — 返回字符串 haystack 中 needle 最后一次出现的数字位置。
```
php
strrpos
(
string
$haystack
,
string
$needle
,
int
$offset
=
0
)
:
int
var_dump
(
strrpos
(
'Hello, World'
,
'o'
));
// int(8)
```
5、替换函数
str_replace — 子字符串替换, 该字符串或数组是将 subject 中全部的 search 都被 replace 替换之后的结果。
```
php
str_replace
(
mixed
$search
,
mixed
$replace
,
mixed
$subject
,
int
&
$count
=
?
)
:
mixed
var_dump
(
str_replace
(
'o'
,
'A'
,
'Hello, World'
));
// string(12) "HellA, WArld"
```
6、格式化函数
printf — 输出格式化字符串
```
php
printf
(
string
$format
,
mixed
$args
=
?
,
mixed
$
...
=
?
)
:
int
```
sprintf — 返回格式化字符串
```
php
sprintf
(
string
$format
,
mixed
...
$values
)
:
string
```
格式化参数:
[
https://www.php.net/manual/zh/function.sprintf.php
](
https://www.php.net/manual/zh/function.sprintf.php
)
```
php
// 直接输出,返回数字
$ret1
=
printf
(
'my name is %s, and year old is %d'
,
'Tom'
,
18
);
// my name is Tom, and year old is 18
var_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 — 重复一个字符串
```
php
str_repeat
(
string
$input
,
int
$multiplier
)
:
string
echo
str_repeat
(
'ABC'
,
5
);
// ABCABCABCABCABC
```
str_shuffle — 随机打乱一个字符串
```
php
str_shuffle
(
string
$str
)
:
string
echo
str_shuffle
(
'ABCDEFG'
);
// GBEACDF
```
https://www.bilibili.com/video/BV18x411H7qD?p=81&spm_id_from=pageDriver
doc/miniprogram.md
浏览文件 @
ece1651d
...
@@ -17,3 +17,7 @@
...
@@ -17,3 +17,7 @@
[
mpvue
](
https://mpvue.com/
)
: 一个使用 Vue.js 开发小程序的前端框架。
[
mpvue
](
https://mpvue.com/
)
: 一个使用 Vue.js 开发小程序的前端框架。
[
Vue Mini
](
https://vuemini.org/guide/
)
是一个基于 Vue 3 的小程序开发库,它能让你用 Composition API 写小程序。
[
Vue Mini
](
https://vuemini.org/guide/
)
是一个基于 Vue 3 的小程序开发库,它能让你用 Composition API 写小程序。
[
weapp-qrcode
](
https://github.com/tomfriwel/weapp-qrcode
)
微信小程序生成二维码工具
[
echarts-map
](
https://github.com/mouday/echarts-map
)
包含 echarts 用到的 map 地图文件
\ No newline at end of file
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录