PHP在字符串中的每个数字上使用preg_replace

Say I have a multiline string like

A: 51
B: 221
C: 45

And I want to replace it to get

A: $51
B: $221
C: $45

I tried

preg_replace("/[\d*][^0-9]*/","$currency$0",$pricelist);

but that prepends the currency symbol before every digit instead of every number. I also tried

preg_replace("/[\d]*/","$currency$0",$pricelist);

but that surrounds the amount with two currency symbols.

Use + quantifier instead of *:

preg_replace("/\d+/", "$currency$0",$pricelist);

Using * quantifier, your regex first matches all the digits, and then matches the empty string after the last digit. Hence you see two $ symbols - One before the digits matched, and one before the empty string matched after last digit.