php删除字符串末尾指定字符
时间:2016-11-04
有时候我们需要删除一个字符串中的指定字符,而且只有该字符在末尾时才会删除,在其他位置出现的该字符则不删除,这时候我们该如何使用php代码来实现呢?具体源码请看下面讲解。
需求如下:
删除在末尾出现的字符串"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));
- php概述
- php教程
- php环境搭建
- PHP书写格式
- php变量
- php常量
- PHP注释
- php数组
- php字符串 string
- PHP整型 integer
- PHP浮点型 float
- php布尔型
- php数据类型之数组
- php数据类型之对象
- php数据类型之null
- php数据类型之间的转换
- php运算符
- php表达式
- PHP循环控制
- PHP流程控制
- php函数
- php全局变量
- PHP魔术变量
- php命名空间
- php 日期
- PHP包含文件
- php文件
- PHP 文件上传
- php Cookies
- php Sessions
- php email
- php安全email
- php错误处理
- PHP异常处理
- php过滤器
- PHP 高级过滤器
- php json
- php 表单
- PHP MySQL 简介
- PHP 连接 MySQL
- php创建数据库
- php 创建表
- php mysq 插入数据
- PHP MySQL 插入多条数据
- PHP MySQL 预处理语句
- php mysql 读取数据
- php mysql where
- PHP MySQL Order By
- PHP MySQL Update
- PHP MySQL Delete
- php ODBC
- 第006课 开发板熟悉与体验
- Angular Observable数据类型的单元测试数据准备
- 第007课 裸机开发步骤和工具使用(SourceInght NotePad++使用)
- Angular jasmine.expect单步调试
- 第008课 第1个ARM裸板程序及引申(点亮LED灯)
- SharedPreferences VS MMKV
- 第009课 gcc和arm-linux-gcc和Makefile
- Go 每日一库之 quicktemplate
- 第010课 掌握Jz2440_ARM芯片时钟体系
- 第011课 Jz2400串口(UART)的使用
- 面试官看完我手写的单例直接惊呆了!
- 安利几个JS开发小技巧
- 深入理解Pod(一)
- [902]python list排序
- 第012课 内存控制器与SDRAM