1

我被要求从成分表中获取数量和成分。我已经成功地从列表中提取了数量和成分,但我被困在出现半个字符的地方,例如 ¼、½ 等。它们没有用 HTML 编码,我的正则表达式在那里失败。

这是成分清单

$subject = array("1 teaspoon salt",
"¼ teaspoon black pepper",
"1 cup all-purpose flour",
"1 ½ - 2 cups shredded Parmesan cheese");

正则表达式

 preg_replace ('/(([0-9][\s+]*[\-]*[0-9]*[\s+]*)(teaspoon|tablespoons|cup|cups)*)([a-z0-9\s]+)/','Quantity: $1 Name: $4',$food)

输出

 Quantity: 1 teaspoon Name: salt
 ¼ teaspoon black pepper (failed)
 Quantity: 1 cup Name: all-purpose flour
 Quantity: 1 Name: ½ - Quantity: 2 cup Name: s shredded Parmesan cheese (failed)
4

1 回答 1

0

Unicode 代码点合并到您的正则表达式中。乍一看,看起来适当的代码点是[\x{00BC}-\x{00BE}\x{2153}-\x{215E}] 您还必须使用u修饰符才能在 preg 中启用 unicode。

于 2013-01-15T00:53:40.800 回答