I got a different strings with lists of percentages:
'51% 20% 19% 6% 3% 2% 2% ...'
How can I cut the string to:
'51% 20% 19%'
Maybe some regex?
I got a different strings with lists of percentages:
'51% 20% 19% 6% 3% 2% 2% ...'
How can I cut the string to:
'51% 20% 19%'
Maybe some regex?
收起
You could match up until the 3
rd occurrence of the percent sign.
$str = '51% 20% 19% 6% 3% 2% 2%';
preg_match('/^(?:[^%]*%){3}/', $str, $match);
echo $match[0]; //=> "51% 20% 19%"
A verbose way would be..
$str = '51% 20% 19% 6% 3% 2% 2%';
$val = explode(' ', $str);
echo "$val[0] $val[1] $val[2]"; //=> "51% 20% 19%"
报告相同问题?