我找到了oposite的解决方案:“在第二个” - “字符出现之后将所有字符串删除?”
$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));
没用。
答案 0 :(得分:3)
$testString = "Today is - Friday and tomorrow is - Saturday";
$newString = substr(strstr(substr(strstr($testString, "-"),1),"-"),1);
echo $newString;//print Saturday
在第一个-
之后找到子字符串,然后在第一个-
之后找到子字符串。
答案 1 :(得分: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;