我正试图从字符串中删除"/"之后的所有数据
$price="10/3"
我已经尝试了preg_replace
$str = '2016/19';
$change = str_replace('/','-',$str);
$pattern = '/-*/';
$new = preg_replace($pattern,'',$change);
我试着按照上面的方式来做,因为不知道斜杠是否有问题,所以我把字符串改为2016-19,然后试图替换模式。但它并没有删除后面的数据-它只是删除了-
此外,我不能做子字符串,因为在/之前和之后更改的位数
你几乎是对的。
$str = '2016/19';
// escape "/" by using "'/"
// .*$ matches any character up to the end of the string denoted by "$"
$pattern = '/'/.*$/';
$new = preg_replace($pattern,'',$str);
echo $new;
$str = '2016/19';
$result = (explode("/", $str)[0]); //get the part before "/" after splitting
http://php.net/manual/en/function.explode.php