1

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.

1 Answer 1

4

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.