我已经有了一个函数来计算字符串中的项目数($ paragraph)并告诉我结果有多少个字符,即tsp和tbsp的当前值是7,我可以用它来计算出该字符串的百分比。Preg匹配并以短字符串计算结果匹配
我需要的preg_match加强这一点,因为10tsp应为5
$characters = strlen($paragraph);
$items = array("tsp", "tbsp", "tbs");
$count = 0;
foreach($items as $item) {
//Count the number of times the formatting is in the paragraph
$countitems = substr_count($paragraph, $item);
$countlength= (strlen($item)*$countitems);
$count = $count+$countlength;
}
$overallpercent = ((100/$characters)*$count);
我知道这会是这样的preg_match('#[d]+[item]#', $paragraph)
右算什么?
编辑对于曲线球感到遗憾,但数字和$ item之间可能有空格,一个preg_match可以捕获两个实例吗?
不太清楚你需要什么解析....“tsptbsptbs ...”或“5tbs3tsp ..”?你能举几个例子和预期的结果吗? –
'10tsp'>> 5 || '1tsp'>> 4 || '1茶匙'>> 5 || '1茶匙和2茶匙'>> 10 ||那有意义吗?只是字符数组中的事物的匹配,但也包括之前的数字(有/没有空格) – bluedaniel