首页 > 解决方案 > 子串提取。获取最后一个'/'之前的字符串

问题描述

我正在尝试提取子字符串。我需要一些帮助来用 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 方法处理这种情况?

标签: phpstringsubstring

解决方案


您可以使用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

推荐阅读