当前位置:网站首页>PHP intercepts the contents before and after the specified string
PHP intercepts the contents before and after the specified string
2022-07-21 20:51:00 【It's Anbin】
Suppose there is now a string as follows :
$string = ' Ha ha ha ha anbin learning php Hey, hey, hey ';
Demand is , You need to put the string anbin Remove from the string , The result of the string is :' Hahaha, learning php Hey, hey, hey '.
At first glance, it's actually very simple , Use it directly php Built in functions str_replace () that will do .
The method is as follows :
echo str_replace('anbin','',$string);
But now I want to get the string anbin Previous string : ' Ha ha ha ha ' And string anbin String after : ' learning php Hey, hey, hey '. Now str_replace () Can't meet the demand , What to do ?
The method is as follows :
/*
@param $string A string of strings
@param $removeStr $string A string contained in
*/
function getRemoveStr($string,$removeStr){
$prevSeat = strpos($string,$removeStr); // obtain $remove stay $string Position in
$prevString = substr($string,0,$prevSeat); // Intercept from the beginning to $remove Previous string
$zongSeat = $prevSeat + strlen($removeStr); // Splicing a wave $removeStr The character length of itself gets the starting position of the string to be intercepted in the second half
$nextString = substr($string,$zongSeat); // The string intercepted from the beginning to the end of the second half of the string
$endString = $prevString . $nextString; // hold $removerStr Previous string and $removerStr The following strings are spliced , Get rid of $removerStr String other than
return $endString;
}
echo getRemoveStr($string,'anbin');
This method can not only get character string anbin Previous string , You can also get character string anbin String after , It can also be spliced . Although the efficiency is slower than the first , But the function is really rich . Finally, you need to meet your own needs , Make a wheel by yourself !
边栏推荐
- 推荐一个好用的 所见即所得的 markdown 编辑器 Mark Text
- 通过Web代理服务器实现文本文件传输至外部服务器并且修改后返回
- Getting started with mobile security
- Buuctf [gxyctf2019] no dolls
- Laravel5.1 下的计划任务
- The text file is transferred to the external server through the web proxy server and returned after modification
- Common formula for matrix derivation (avoiding pit) + derivation of matrix modulus and absolute value
- tp5导入excel到数据库
- 【极客大挑战 2019】Easy,Love,Baby-SQL
- 推薦一個好用的 所見即所得的 markdown 編輯器 Mark Text
猜你喜欢
BUUCTF-web-随便注
thinkphp6使用EasyWeChat5.x公众号开发(二)
12. [i/o flow get() and getline() and put() functions]
Buuctf [gxyctf2019] no dolls
DVWA [SQL injection] error injection learning record
phpmyadmin后台文件包含漏洞分析
Openfoam programming: combination of VOF method and porous media model
vulnhub-DC-4靶机渗透记录
Buuctf n1book [Chapter 2 advanced web] file upload
【极客大挑战 2019】Easy,Love,Baby-SQL
随机推荐
COMSOL heat conduction method to solve maze problem (path planning)
web安全--文件包含(本地包含,远程包含)
thinkphp6使用EasyWeChat5.x公众号开发(二)
Usage and purpose of as unknown as XXX in typescript
OpenFOAM编程:VOF法与多孔介质模型相结合
大文件下载 解决方案
Cannot read property mode of undefined
BUUCTF n1book [第二章 web进阶]文件上传
ThinkPHP6 学习心得
Nodejs reads and parses the dom of XML
Global variable configuration dev . dev.dev . dev.test
Swift navigation bar color device and remove the lower edge line
Wechat applet request:fail -2:net:: err_ FAILED
Comsol热传导方法求解迷宫问题(路径规划)
DVWA [SQL injection (blind)] learning record
How is redis different from memcached
php百度人脸检测api测颜值评分(源码直接可用)
1. Vite acquaintance and vite construction project
Usage and introduction of sonarlint
使用Curl 实现 本地 以及远程设备的请求