获取第一个和第三个字符之间的字符串


Get string between first and third occurence of character

我有很多字符串如下所示:QR-DF-6549-1和QR-DF-6529

我想得到字符串的以下部分:DF-6549

编辑:所以我也想去掉结尾的"-1",以防它存在。

如何使用php实现这一点?我知道substr,但我对这个有点不知所措。

非常感谢!

给定样本数据,正则表达式可能是最好的方法

//  ^    start matching at start of string
//  [A-Z]{2}-  must start with two capital letters and a dash
//  (    we want to capture everything that follows
//  [A-Z]{2}-  next part must start with two capital letters and a dash
//  'd+  a sequence of one or more digits
//  )    end the capture - this will be index 1 in the $match array allowed
if (preg_match('/^[A-Z]{2}-([A-Z]{2}-'d+)/', $str, $match)) {
    $data=$match[1];
}