本文记录PHP处理字符串的方法,包括普通字符串处理以及正则表达式处理字符串方法。
更详细PHP语法和函数介绍可参考官方文档:https://www.php.net/manual/zh/,本文仅记录了部分我使用到的一些方法。
本文使用的PHP版本为PHP 8.1.1
PHP基本语法
1、PHP语言标记
2、PHP代码可以嵌入到html代码中
3、使用分号分隔指令,文件末尾的 PHP 代码段结束标记可以不要,结束标志 ?>
隐含了一个分号。
4、注释
示例代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| <?php echo 'hello world!' ?>
<!DOCTYPE html> <html lang="zh-CN">
<head> <meta charset="utf-8" /> <title> PHP Demo <?php echo '演示'; ?></title> </head>
<body> <br /> <?php echo 'To the time to life, ' ?> <br /> <?php echo 'rather than to life in time.';
?> </body>
</html>
|
浏览器访问地址 http://localhost:8081/demo/
字符串处理
PHP提供了很多字符串处理相关函数,这里记录我使用到的部分方法。
1. 字符串分割
语法:explode(string $separator
, string $string
, int $limit
= PHP_INT_MAX
): array,返回字符串数组
举例:
1 2 3 4 5 6 7 8 9 10 11 12 13
| <?php $str = 'one,two,three'; print_r(explode(',',$str)); echo "<br/>";
print_r(explode(',',$str,2)); echo "<br/>";
$str1 = 'one two three'; print_r(explode(PHP_EOL,$str1)); ?>
|
结果:
1 2 3
| Array ( [0] => one [1] => two [2] => three ) Array ( [0] => one [1] => two,three ) Array ( [0] => one [1] => two [2] => three )
|
2. 用字符串连接数组元素
implode支持将数组元素拼接成字符串:implode(string $separator
, array $array
): string
举例:
1 2 3 4 5 6
| <?php
$array = ['one', 'two', 'three']; print_r(implode(",", $array));
?>
|
结果:
3. 字符串切片
使用 substr
方法来返回字符串的子串。
- substr(
$string
, $offset
, $length
= null
)
举例:
1 2 3 4 5 6 7 8 9 10 11
| <?php
$str = 'abcdefgh'; echo substr($str, -2) . "<br />\n"; echo substr($str, -2, 1) . "<br />\n"; echo substr($str, 0, 1) . "<br />\n"; echo substr($str, 0, -1) . "<br />\n"; echo substr($str, 2, 3) . "<br />\n"; echo substr($str, -3, -1) . "<br />\n";
?>
|
4. 字符替换
str_replace(mixed $search
, mixed $replace
, mixed $subject
, int &$count
= ?): mixed - 字符替换,区分大小写。
str_ireplace() - 不区分大小写的替换。
举例:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| <?php
$str="one,two,three"; print_r(str_replace('t','T',$str,$count)); echo "<br/>"; print_r($count); echo "<br/>";
$str="one,two,Three"; print_r(str_ireplace('T','3',$str,$count)); echo "<br/>";
?>
|
结果:
1 2 3
| one,Two,Three 2 one,3wo,3hree
|
5. 大小写转换
常用大小写转换方法:
举例:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| <?php
$str = 'One,Two,Three'; echo strtolower($str) . "<br />\n";
$str = 'one,two,three'; echo strtoupper($str) . "<br />\n";
$str = 'one,two,three'; echo ucfirst($str) . "<br />\n";
$str = 'One,Two,Three'; echo lcfirst($str) . "<br />\n";
$str = 'one,two,three'; echo ucwords($str,',') . "<br />\n";
?>
|
结果:
1 2 3 4 5
| one,two,three ONE,TWO,THREE One,two,three one,Two,Three One,Two,Three
|
6. 字符ASCII 码相互转换
① 字符转ASCII 码:ord($string
)
② ASCII 码转字符:
举例:
1 2 3 4 5 6 7 8 9 10 11 12
| <?php
$str = 'A'; echo "Char: ". $str .", ASCII: " .ord($str) . "<br />\n";
echo "Char: ". chr(ord($str) + 1) . "<br />\n";
echo sprintf("Char: %c <br />\n", ord($str) + 2);
?>
|
结果:
1 2 3
| Char: A, ASCII: 65 Char: B Char: C
|
7. 将字符串解析成多个变量
parse_str - 一般用于解析URL中的参数。
举例:
1 2 3 4 5 6 7 8 9 10 11
| <?php
$str = 'one=1&two=2&arr[]=3&arr[]=4'; parse_str($str, $output);
print_r($output); echo "<br />\n"; print_r($output['arr']); echo "<br />\n";
?>
|
结果:
1 2
| Array ( [one] => 1 [two] => 2 [arr] => Array ( [0] => 3 [1] => 4 ) ) Array ( [0] => 3 [1] => 4 )
|
8. 字符查询
几种字符串查询方法:
- substr_count(
$haystack
, $needle
, $offset
= 0, $length
= null
) - 计算子串出现的次数
- strstr(
$haystack
, $needle
, $before_needle
= false
) - 查找字符串的首次出现
- strrchr(
$haystack
, $needle
) - 查找指定字符在字符串中的最后一次出现
- strpos(
$haystack
, $needle
, $offset
= 0) - 查找字符串首次出现的位置(区分大小写)
- stripos(
$haystack
, $needle
, $offset
= 0) - 查找字符串首次出现的位置(不区分大小写)
- strrpos(
$haystack
, $needle
, $offset
= 0) - 计算指定字符串在目标字符串中最后一次出现的位置(区分大小写)
- strripos(
$haystack
, $needle
, $offset
= 0) - 计算指定字符串在目标字符串中最后一次出现的位置(不区分大小写)
- strpbrk(
$haystack
, $char_list
) - 在字符串中查找一组字符的任何一个字符
举例:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
| <?php
$str = 'one,two,three'; echo "substr_count: " . substr_count($str,',') . "<br />\n";
$str = 'one,two,three'; echo "strstr: " . strstr($str,',') . "<br />\n"; echo "strstr: " . strstr($str,',', true) . "<br />\n";
$str = 'one,two,three'; echo "strrchr: " . strrchr($str,',') . "<br />\n";
$str = 'one,two,Three'; echo "strpos: " . strpos($str,'T') . "<br />\n";
$str = 'one,two,Three'; echo "stripos: " . stripos($str,'T') . "<br />\n";
$str = 'one,two,Three'; echo "strrpos: " . strrpos($str,'t') . "<br />\n";
$str = 'one,two,Three'; echo "strripos: " . strripos($str,'t') . "<br />\n";
$str = 'one,two,Three'; echo "strpbrk: " . strpbrk($str,'nt') . "<br />\n";
?>
|
结果:
1 2 3 4 5 6 7 8 9
| substr_count: 2 strstr: ,two,three strstr: one strrchr: ,three strpos: 8 stripos: 4 strrpos: 4 strripos: 8 strpbrk: ne,two,Three
|
9. HTML字符转换
① htmlspecialchars_decode($string
, $flags
= ENT_COMPAT | ENT_HTML401) — 将特殊的 HTML 实体转换回普通字符
② htmlspecialchars ( $string
, $flags
= ENT_COMPAT | ENT_HTML401, $encoding
= ini_get(“default_charset”), $double_encode
= true
) - 将特殊字符转换为 HTML 实体
举例:
1 2 3 4 5 6
| <?php
$str = '"http://www.baidu.com"'; echo htmlspecialchars_decode($str) . "<br />\n";
?>
|
结果:
1 2 3 4
| <?php "http://www.baidu.com" ?>
|
10. 去除HTML标签
1 2
| $str = '<strong>http://www.baidu.com</strong>'; echo strip_tags($str) . "<br />\n";
|
结果:
11. 过滤变量
filter_var - 使用特定的过滤器过滤一个变量
举例:提取字符串中的数字
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| <?php
$str = 'one,two,Three,4'; echo "number: " . filter_var($str, FILTER_SANITIZE_NUMBER_INT) . "<br />\n";
preg_match_all('!\d+!', $str, $matches); echo "<pre>"; print_r($matches); echo "</pre>";
$str2 = preg_replace('/[^0-9]/', '', $str); echo "number: " . $str2 . "<br />\n";
?>
|
结果:
1 2 3 4 5 6 7 8 9 10
| number: 4 Array ( [0] => Array ( [0] => 4 )
) number: 4
|
正则表达式处理字符串
1. 正则匹配
举例:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
| <?php
$str = 'http://www.baidu.com/ https://www.qq.com/'; $pattern = '/(https|http):\/\//'; $isMatched = preg_match($pattern, $str, $matches); echo "<pre>"; print_r($matches); echo "</pre>";
$isMatched = preg_match_all($pattern, $str, $matches); echo "<pre>"; print_r($matches); echo "</pre>";
$isMatched = preg_match_all($pattern, $str, $matches, PREG_SET_ORDER); echo "<pre>"; print_r($matches); echo "</pre>";
$isMatched = preg_match_all($pattern, $str, $matches, PREG_PATTERN_ORDER); echo "<pre>"; print_r($matches); echo "</pre>";
$str = '<img src="/zentao/file-read-95.png" alt="" /><img src="/zentao/file-read-96.png" alt="" />'; $pattern = '/src=\".*?([0-9]+).(.*?)\"/'; preg_match_all($pattern, $str, $diagrams); echo "<pre>"; print_r($diagrams); echo "</pre>";
?>
|
结果:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
| Array ( [0] => http: [1] => http ) Array ( [0] => Array ( [0] => http: [1] => https: )
[1] => Array ( [0] => http [1] => https )
) Array ( [0] => Array ( [0] => http: [1] => http )
[1] => Array ( [0] => https: [1] => https )
) Array ( [0] => Array ( [0] => http: [1] => https: )
[1] => Array ( [0] => http [1] => https )
) Array ( [0] => Array ( [0] => src="/zentao/file-read-95.png" [1] => src="/zentao/file-read-96.png" )
[1] => Array ( [0] => 95 [1] => 96 )
[2] => Array ( [0] => png [1] => png )
)
|
2. 分割字符串
- preg_split(string
$pattern
, string $subject
, int $limit
= -1, int $flags
= 0): array
举例:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| <?php
$str = 'http://www.baidu.com/ https://www.qq.com/'; $pattern = '/(https|http):\/\//'; $chars = preg_split($pattern, $str, -1, PREG_SPLIT_NO_EMPTY); echo "<pre>"; print_r($chars); echo "</pre>";
$chars = preg_split($pattern, $str, -1, PREG_SPLIT_DELIM_CAPTURE); echo "<pre>"; print_r($chars); echo "</pre>";
$chars = preg_split($pattern, $str, -1, PREG_SPLIT_OFFSET_CAPTURE); echo "<pre>"; print_r($chars); echo "</pre>";
?>
|
结果:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
| Array ( [0] => www.baidu.com/ [1] => www.qq.com/ ) Array ( [0] => [1] => http [2] => www.baidu.com/ [3] => https [4] => www.qq.com/ ) Array ( [0] => Array ( [0] => [1] => 0 )
[1] => Array ( [0] => www.baidu.com/ [1] => 7 )
[2] => Array ( [0] => www.qq.com/ [1] => 30 )
)
|
输出:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| Array ( [0] => Array ( [0] => src="/zentao/file-read-95.png" [1] => src="/zentao/file-read-96.png" )
[1] => Array ( [0] => 95 [1] => 96 )
[2] => Array ( [0] => png [1] => png )
)
|
3. 正则替换
preg_replace 可以进行正则表达式的搜索和替换。
举例:
1 2 3 4 5 6 7 8 9 10
| <?php
$str = 'http://www.baidu.com/ https://www.qq.com/'; $pattern = '/(https|http):\/\//'; $replacement = ''; $newStr = preg_replace($pattern, $replacement, $str); echo $newStr . "<br />\n";
?>
|
结果:
1
| www.baidu.com/ www.qq.com/
|
4. 删除多余空格
1 2 3 4 5 6 7 8 9 10
| $str = ' hel lo w or ld ';
$result = trim($str);
$result = rtrim($str);
$result = ltrim($str);
$result = preg_replace('/\s+/', '', $str);
|
5. 非贪婪匹配-匹配1次
非贪婪匹配会尽可能少地匹配字符,使用 ?
来表示非贪婪匹配。
1 2 3 4 5
| $string = 'a) Hello, World! test).';
$pattern = '/(.*?)\)/'; preg_match($pattern, $string, $matches);
|
参考资料:
- 字符串函数:https://www.php.net/manual/zh/ref.strings.php
- 正则表达式函数:https://www.php.net/manual/zh/ref.pcre.php
- PHP官方文档:https://www.php.net/manual/zh/
--THE END--