1

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

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

现在,我需要“BEFORE”第二个“-”的解决方案:

Today is - Friday and tomorrow is - Saturday

成为:

Saturday

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

我试过类似的东西:

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

没用。

4

2 回答 2

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

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

于 2013-07-10T02:05:18.643 回答
0

使用爆炸的替代方法。

  $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;
于 2013-07-10T02:18:55.843 回答