剥去字符串中出现的第二个“-”字符之前的所有内容


Strip everything in string before the second “-” character that occurs?

我找到了opsite的解决方案:"在第二个"-"出现的字符之后剥离字符串中的所有内容?"

$newstr = substr($str, 0, strpos($str, '-', strpos($str, '-')+2));

现在,我需要解决"之前"的第二个"-":

Today is - Friday and tomorrow is - Saturday

未来:

Saturday

如果可能的话,我想用strstr()来做这件事。

我试过类似的东西:

$newstr = substr($str, 0, strstr($str, '-', strstr($str, '-')+2));

没用。

$testString = "Today is - Friday and tomorrow is - Saturday";
$newString = substr(strstr(substr(strstr($testString, "-"),1),"-"),1);
echo $newString;//print  Saturday

查找第一个-之后的子字符串,然后查找其中第一个-之后的子串。

使用爆炸的替代方法。

  $str = "Today is - Friday and tomorrow is - Saturday";
  $parts = explode("-", $str, 3);
  if (count($parts) == 3){
        $res = array_pop($parts);
  } else {
        // handle the case where there is no second delimiter.
        $res = "";
  }
  echo $res;