本文记录PHP处理字符串的方法,包括普通字符串处理以及正则表达式处理字符串方法。
目录更详细PHP语法和函数介绍可参考官方文档:https://www.php.net/manual/zh/,本文仅记录了部分我使用到的一些方法。
PHP基本语法
<?PHP //开始
?> //结束
3、使用分号分隔指令,文件末尾的 PHP 代码段结束标记可以不要,结束标志 ?>
隐含了一个分号。
4、注释
/*
多行注释
*/
//单行注释
示例代码:
<?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,返回字符串数组
举例:
<?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));
?>
结果:
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
举例:
<?PHP
/* implode */
$array = ['one', 'two', 'three'];
print_r(implode(",", $array));
?>
结果:
one,two,three
3. 字符串切片
使用 substr
方法来返回字符串的子串。
- substr(
$string
,$offset
,$length
=null
)
举例:
<?PHP
/* substr */
$str = 'abcdefgh';
echo substr($str, -2) . "<br />\n"; // gh
echo substr($str, -2, 1) . "<br />\n"; // g
echo substr($str, 0, 1) . "<br />\n"; // a
echo substr($str, 0, -1) . "<br />\n"; // abcdefg
echo substr($str, 2, 3) . "<br />\n"; // cde
echo substr($str, -3, -1) . "<br />\n"; // fg
?>
4. 字符替换
str_replace(mixed $search
, mixed $replace
, mixed $subject
, int &$count
= ?): mixed - 字符替换,区分大小写。
str_ireplace() - 不区分大小写的替换。
举例:
<?PHP
/* str_replace */
$str="one,two,three";
print_r(str_replace('t','T',$str,$count));
echo "<br/>";
print_r($count);
echo "<br/>";
/* str_ireplace */
$str="one,two,Three";
print_r(str_ireplace('T','3',$str,$count));
echo "<br/>";
?>
结果:
one,Two,Three
2
one,3wo,3hree
5. 大小写转换
常用大小写转换方法:
- strtolower - 将字母字符转换为小写。
- strtoupper - 将字母字符转换为大写。
- ucfirst - 首字母转换为大写。
- lcfirst - 首字母转换为小写。
- ucwords - 将字符串每个单词的首字符转换成大写。
举例:
<?PHP
/* strtolower */
$str = 'One,Two,Three';
echo strtolower($str) . "<br />\n";
/* strtoupper */
$str = 'one,two,three';
echo strtoupper($str) . "<br />\n";
/* ucfirst */
$str = 'one,two,three';
echo ucfirst($str) . "<br />\n";
/* lcfirst */
$str = 'One,Two,Three';
echo lcfirst($str) . "<br />\n";
/* ucwords */
$str = 'one,two,three';
echo ucwords($str,',') . "<br />\n";
?>
结果:
one,two,three
ONE,TWO,THREE
One,two,three
one,Two,Three
One,Two,Three
6. 字符ASCII 码相互转换
① 字符转ASCII 码:ord($string
)
② ASCII 码转字符:
举例:
<?PHP
/* ord */
$str = 'A';
echo "Char: ". $str .", ASCII: " .ord($str) . "<br />\n";
/* chr */
echo "Char: ". chr(ord($str) + 1) . "<br />\n";
/* sprintf */
echo sprintf("Char: %c <br />\n", ord($str) + 2);
?>
结果:
Char: A, ASCII: 65
Char: B
Char: C
7. 将字符串解析成多个变量
parse_str - 一般用于解析URL中的参数。
举例:
<?PHP
/* parse_str */
$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";
?>
结果:
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
) - 在字符串中查找一组字符的任何一个字符
举例:
<?PHP
/* substr_count */
$str = 'one,two,three';
echo "substr_count: " . substr_count($str,',') . "<br />\n";
/* strstr */
$str = 'one,two,three';
echo "strstr: " . strstr($str,',') . "<br />\n";
echo "strstr: " . strstr($str,',', true) . "<br />\n";
/* strrchr */
$str = 'one,two,three';
echo "strrchr: " . strrchr($str,',') . "<br />\n";
/* strpos */
$str = 'one,two,Three';
echo "strpos: " . strpos($str,'T') . "<br />\n";
/* stripos */
$str = 'one,two,Three';
echo "stripos: " . stripos($str,'T') . "<br />\n";
/* strrpos */
$str = 'one,two,Three';
echo "strrpos: " . strrpos($str,'t') . "<br />\n";
/* strripos */
$str = 'one,two,Three';
echo "strripos: " . strripos($str,'t') . "<br />\n";
/* strpbrk */
$str = 'one,two,Three';
echo "strpbrk: " . strpbrk($str,'nt') . "<br />\n";
?>
结果:
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 实体
举例:
<?PHP
/* htmlspecialchars_decode */
$str = '"http://www.baidu.com"';
echo htmlspecialchars_decode($str) . "<br />\n";
?>
结果:
<?PHP
"http://www.baidu.com"
?>
10. 过滤变量
filter_var - 使用特定的过滤器过滤一个变量
举例:提取字符串中的数字
<?PHP
// 方法1
$str = 'one,two,Three,4';
echo "number: " . filter_var($str, FILTER_SANITIZE_NUMBER_INT) . "<br />\n";
// 方法2
preg_match_all('!\d+!', $str, $matches);
echo "<pre>";
print_r($matches);
echo "</pre>";
// 方法3
$str2 = preg_replace('/[^0-9]/', '', $str);
echo "number: " . $str2 . "<br />\n";
?>
结果:
number: 4
Array
(
[0] => Array
(
[0] => 4
)
)
number: 4
正则表达式处理字符串
1. 正则匹配
- preg_match - 正则表达式匹配
- preg_match_all - 全局正则表达式匹配
举例:
<?PHP
/* preg_match */
$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>";
/* preg_match_all */
$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>";
?>
结果:
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
举例:
<?PHP
/* preg_split */
$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>";
?>
结果:
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
)
)
输出:
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 可以进行正则表达式的搜索和替换。
举例:
<?PHP
/* preg_replace */
$str = 'http://www.baidu.com/ https://www.qq.com/';
$pattern = '/(https|http):\/\//';
$replacement = '';
$newStr = preg_replace($pattern, $replacement, $str);
echo $newStr . "<br />\n";
?>
结果:
www.baidu.com/ www.qq.com/
参考资料:
- 字符串函数: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/
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。