当前位置:网站首页>PHP字符串操作
PHP字符串操作
2022-07-27 16:22:00 【Miracle Fan】
字符串相关操作
文章目录
1.单双引号定义字符串
双引号的内容是经过PHP解析得到的;而单引号就是所见即所得
<?php
$test="PHP";
$str1="I Like $test";
$str2='I Like $test';
echo $str1;
echo "<br>";
echo $str2;
?>
//I Like PHP
//I Like $test
2. 字符串操作
2.1 去除首尾空格和特殊字符
(1)trim()函数
用于去除字符串首尾处的空白字符(或其他字符)
trim(string str,string charlist)
str为操作的字符串对象,charlist为可选参数,希望过滤的字符。如果不指定字符,则默认取出如下字符。
| 参数值 | 说明 |
|---|---|
| \0 | null |
| \t | tab,制表符 |
| \n | 换行符 |
| \x0B | 垂直制表符 |
| \r | 回车符 |
| “ ” | 空格 |
(2)ltrim()函数
去除字符串左侧字符
ltrim(string str,string charlist)
(2)rtrim()函数
去除字符串右侧字符
rtrim(string str,string charlist)
<?php
$str='$I Like PHP.html$';
echo trim($str,'$');
echo"<br>";
echo ltrim($str,'$');
echo"<br>";
echo rtrim($str,'$');
?>
<!--I Like PHP.html
I Like PHP.html$
$I Like PHP.html-->
2.2 获取字符串的长度
strlen(string str)
输出指定字符串长度
<?php
$str2='I Like PHP';
echo strlen($str2);
?>
//12
注 意 {\color{Red}注意} 注意:汉字占两个字符,其他都占1个字符
2.3 截取字符串
substr(string str,int start,int length)
| 参数 | 说明 |
|---|---|
| str | 指定字符串对象 |
| start | 指定开始截取字符串位置。若start为负数,则从末尾开始截取 |
| length | 指定截取字符串个数,为负数,则倒数第length字符串 |
<?php
$str='ILikePHP';
echo substr($str,0);
echo"<br>";
echo substr($str,1,7);
echo"<br>";
echo substr($str,-4,4);
?>
//ILikePHP
//LikePHP
//ePHP
2.4 检索字符串
1.strstr()函数
获取指定字符串在另一个字符串中首次出现的位置到后者末尾的子字符串
strstr(string haystack,string needle)
| 参数 | 说明 |
|---|---|
| haystack | 必要参数,指定字符串 |
| needle | 指定字符,如果为数值则为对应ASCII值匹配字符 |
<?php
$str='ILikePHP.html';
echo strstr($str,'.');
?>
//.html获取文件后缀名
2. substr_count()函数
返回检索字符串出现次数
substr_count(string haystack,string needle)
<?php
$str='ILikePHP.html';
echo substr_count($str,'P');
?>
//2
2.5 替换字符串
实现对指定字符串进行替换
1.str_ireplace()函数
使用新的子字符串替换原始的字符串被指定要替换的字符串
str_ireplace(mixed search,mixed replace,mixed subject,int count)
| 参数 | 说明 |
|---|---|
| search | 要搜索的值,可以用array提供多个值 |
| replace | 指定替换的值 |
| subject | 被搜索和替换的字符串或数组 |
| count | 可选参数,执行替换的数量 |
<?php
$str1="P";
$str2="*";
$str='I Like PHP.html';
echo str_ireplace($str1,$str2,$str);
?>
<!--I Like *H*.html-->
2. substr_replace()函数
substr_replace(mixed string,mixed repl,mixed start,mixed length)
<?php
$str1="***";
$str='I Like PHP.html';
$count = 3;
echo substr_replace($str,$str1,7,$count);
?>
<!--I Like ***.html-->
2.6分割、合成字符串
1. explode()函数
explode(string delimiter,string str ,int limit)
<?php
$str='I Like PHP.html';
$str_arr=explode(' ',$str);
print_r($str_arr);
echo "<br>";
echo $str_arr[0];
echo "<br>";
echo $str_arr[1];
echo "<br>";
echo $str_arr[2];
?>
<!--Array ( [0] => I [1] => Like [2] => PHP.html )
I
Like
PHP.html-->
2. implode()函数
implode (string glue,array pieces)
<?php
$str='I Like PHP.html';
$str_arr=explode(' ',$str);
print_r($str_arr);
echo"<br>";
echo implode(" ",$str_arr)
?>
<!--Array ( [0] => I [1] => Like [2] => PHP.html )
I Like PHP.html-->
边栏推荐
- PyGame aircraft war game background implementation
- Sentinel1.8.4 persistent Nacos configuration
- Music rhythm colorful gradient lamp chip -- dlt8s04a- Jericho
- 建木持续集成平台v2.5.2发布
- MySQL set validate_ Adding skip grant tables after password=off failed to start the service
- 飞机大战敌机出场
- Talking about JVM (frequent interview)
- Docker - docker installation, MySQL installation on docker, and project deployment on docker
- ValueError: Found input variables with inconsistent numbers of samples: [80019456, 26673152]【报错】
- 商品评论信息与评论信息分类
猜你喜欢

MySQL 06 事务、视图、索引、备份和恢复

Nodejs 模板引擎ejs

Product recommendation and classified product recommendation

Uni app form submit button send request

Matplotlib (basic usage)

v-if,v-else,v-for

Sentinel1.8.4 persistent Nacos configuration

express

Jianmu continuous integration platform v2.5.2 release

Blog Garden beautification tutorial
随机推荐
Nodejs template engine EJS
Openstack login dashboard prompt authentication error
MySQL 01 关系型数据库设计
Uploading and downloading of files
Jianmu continuous integration platform v2.5.2 release
Order submission
Interceptor拦截器
你想得到想不到的MySQL面试题都在这了(2022最新版)
瑞吉外卖笔记
express
Valueerror: found input variables with inconsistent numbers of samples: [80019456, 26673152] [error reporting]
`this.$emit` 子组件给父组件传递多个参数
Typescript installation
[wechat applet] project practice - lottery application
MySQL set validate_ Adding skip grant tables after password=off failed to start the service
Uni app label jump
百度地图技术概述,及基本API与WebApi的应用开发
MySQL 05 存储过程
Extension of ES6 value
Netred RGB mirror light touch chip-dlt8s15b-jericho