需求如下:
删除在末尾出现的字符串"string",其他位置出现的“string”则保留, 比如:
\"this is a test string\" -> \"this is a test \"
\"this string is a test string\" - > \"this string is a test \"
\"this string is a test\" -> \"this string is a test\"
该如何实现呢?这里有两种实现方法。
1、使用preg_replace函数
具体源码如下:
$new_str = preg_replace(\'/string$/\', \'\', $str);
2、使用substr函数
源码如下:
$substring = \'string\';
/* http://www.manongjc.com/article/1588.html */
$str = \"this string is a test string\";
if (substr($str,-strlen($substring))===$substring)
$str = substr($str, 0, strlen($str)-strlen($substring)); 继续阅读与本文标签相同的文章
-
PS保存图片提示“无法完成请求”,这里有4种解决方法!
2026-05-14栏目: 教程
-
想买1000元左右的5G手机?我们需要等多久?
2026-05-14栏目: 教程
-
剧情反转?美企主动购买华为5G技术,华为成赞赏排行榜第一名!
2026-05-14栏目: 教程
-
城市数字化后,新一代内生安全系统可全方位保护
2026-05-14栏目: 教程
-
谷歌也来“唱衰”5G,5G手机只会徒增功耗?为何这么说?
2026-05-14栏目: 教程
