Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP

Pontus Bolmér
Pontus Bolmér
12,471 Points

Regexp PHP

Im having a question regarding regExp php, i would be very happy if anyone knows the answer to my question.

If im doing the preg_replace function

$numbes = "abc123def";

$numbes = preg_replace("/\d/","897",$numbes);

it gives me the answerr "We love Mankeysabc897897897def"

but if i add i + sign after the d it just gives me one change.

If i add the * it gives me the answer"We love Mankeys897a897b897c897897d897e897f897"

My question is, why does only add in 897 once with the + sign, and with out any + or * it adds several, and with the * it adds so many.

I know it's a wierd question, but im really searcing for a answer.

1 Answer

The \d on its own matches any single digit, so replaces each 1,2,3 with 897.

The \d+ matches one or more digits so it matches the 123 and replaces the group with 897

The \d* matches zero or more digits so the gap between every character matches and is replaced by 897 and the 123 matches so is also replaced by 897