使用strpos查找字符串的第一次出现的位置,然后substr只得到一切到这一位置:
$pos = strpos($str, '-');
if ($pos !== false) { // strpos returns false if there is no needle in the haystack
$str = substr($str, 0, $pos);
}
Run Code Online (Sandbox Code Playgroud)
如果你有PHP 5.3及更高版本,你也可以使用第三个参数设置为true的strstr函数.