php - 子串提取。获取最后一个'/'之前的字符串
问题描述
我正在尝试提取子字符串。我需要一些帮助来用 PHP 做这件事。
以下是我正在使用的一些示例字符串以及我需要的结果:
$temp = "COM1904150001","1","ytuaioeighalk","tyueiff","assets/report/celc_coba/d02c25b2-5c07-11e9-8f1a-02fd8bf7d052/"
我需要的结果是:
$temp = d02c25b2-5c07-11e9-8f1a-02fd8bf7d052
我想在最后一个 '/' 处获取字符串
到目前为止,我已经尝试过:
substr($temp, 0, strpos($temp, '/'))
但是,它似乎根本不起作用。
有没有办法用 PHP 方法处理这种情况?
解决方案
您可以使用substr()
来提取数据,但使用它strrpos()
来查找最后一个/
位置(您必须删除尾随/
才能执行此操作)
$temp = "assets/report/celc_coba/d02c25b2-5c07-11e9-8f1a-02fd8bf7d052/";
// Trim off trailing / or "
$temp = rtrim($temp, "/\"");
// Return from the position of the last / (+1) to the end of the string
$temp = substr($temp, strrpos($temp, '/')+1);
echo $temp;
给...
d02c25b2-5c07-11e9-8f1a-02fd8bf7d052
推荐阅读
- batch-file - 批量删除每个用户目录中的文件
- java - PDFbox从PDF中删除颜色而不显示其背后的颜色
- android - 检测应用何时进入自由窗口模式
- amazon-cloudformation - 如何在 AWS CDK 中引用 `elasticsearch.CfnDomain` 生成的域名?
- arrays - 我的函数快速返回空数组并解析
- java - Java:getter 或零参数方法的功能链接
- javascript - 我想导航到另一个 html 中的特定 div。为什么这段代码不起作用?
- scala - Spark - Add a column which sums another column, grouped by a third column, without losing other columns
- c# - 如何将combobox1项目从form1传递到form2中的combobox2
- c# - CommandLineParser:获取互斥选项集的名称