提交 ece1651d 编写于 作者: 彭世瑜's avatar 彭世瑜

fix

上级 ffa330cc
...@@ -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
...@@ -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.
先完成此消息的编辑!
想要评论请 注册