Replace 4, but not 40

Any questions involving matching text strings to patterns - the pattern is called a "regular expression."

Moderator: General Moderators

Post Reply
tores
Forum Contributor
Posts: 120
Joined: Fri Jun 18, 2004 3:04 am

Replace 4, but not 40

Post by tores »

Hi

How can I modify the following regexp

Code: Select all

echo preg_replace("/,4(,|)/", ",S", "40,4,40,4"); // Outputs 40,S40,S
so it outputs "40,S,40,S".

regards tores
User avatar
anjanesh
DevNet Resident
Posts: 1679
Joined: Sat Dec 06, 2003 9:52 pm
Location: Mumbai, India

Post by anjanesh »

Code: Select all

echo preg_replace("/(^|,)(4)(,|$)/", "\$1S\$3", "40,4,40,4"); // outputs 40,S,40,S
Post Reply