使用通配符删除字符串中的数据
Removing data in a string using wildcard
我正在尝试从字符串中删除“/”之后的所有数据,包括“/”
$price="10/3"
我试过了preg_replace
$str = '2016/19';
$change = str_replace('/','-',$str);
$pattern = '/-*/';
$new = preg_replace($pattern,'',$change);
我尝试按照上面的方式进行操作,因为不知道斜杠是否有问题所以我将字符串更改为 2016-19 然后尝试替换模式但它不会删除 -它只是删除了 -
我也不能做 substr 因为 / 之前和之后的数字数量变化
$str = '2016/19';
$result = (explode("/", $str)[0]); //get the part before "/" after splitting
你几乎是对的。
$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;
我正在尝试从字符串中删除“/”之后的所有数据,包括“/”
$price="10/3"
我试过了preg_replace
$str = '2016/19';
$change = str_replace('/','-',$str);
$pattern = '/-*/';
$new = preg_replace($pattern,'',$change);
我尝试按照上面的方式进行操作,因为不知道斜杠是否有问题所以我将字符串更改为 2016-19 然后尝试替换模式但它不会删除 -它只是删除了 -
我也不能做 substr 因为 / 之前和之后的数字数量变化
$str = '2016/19';
$result = (explode("/", $str)[0]); //get the part before "/" after splitting
你几乎是对的。
$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;