Hi janosh7,
It's not a greediness problem. Your regex matches the first comma, then lazily matches everything, expanding as needed until it finds a 3.
This means it eats up item 2 and item 3.
This fixes it:
Code: Select all
echo preg_replace('~,[^,]+3,~', ',[replaced],', 'item1,item2,item3,item4,item5');
Output: item1,item2,[replaced],item4,item5
It matches a comma, than any number of characters that are not a comma, then a 3 and a comma.
On my tut you can read the "degrees of greed" page for more details about greedy and lazy matching.
Let me know if you have any questions!
Wishing you a fun day.