user3314010
user3314010

Reputation: 41

PHP: Replace words within string

i have following string:

@c24-blue: #005ea8; @c24-green: #737948;

Now i want to replace the definition of @c24-green to #fff.

I have tried it over:

$string = '@c24-blue: #005ea8; @c24-green: #737948;';
$string = preg_replace('/([@c24-green:]) (.*);/', '$1' . ' #fff;', $string);

The result should be:

@c24-blue: #005ea8; @c24-green: #fff;

Is there a solution to get this working?

Regards, Kai

Upvotes: 0

Views: 39

Answers (2)

karthik manchala
karthik manchala

Reputation: 13640

Remove your string "@c24-green:" from character class, following should work..

(@c24-green:) (.*);
 ^         ^

$string = preg_replace('/(@c24-green:) (.*);/', '$1' . ' #fff;', $string);

See DEMO

Edit: If you want to make it generic.. you can use the following:

(@[^:]*:) ([^;]*);

See DEMO

Upvotes: 1

Toto
Toto

Reputation: 91373

How about:

$string = preg_replace('/(@c24-green:)[^;]+/', '$1 #fff', $string);

Upvotes: 1

Related Questions