0

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

0

2 Answers 2

1

How about:

$string = preg_replace('/(@c24-green:)[^;]+/', '$1 #fff', $string);
Sign up to request clarification or add additional context in comments.

Comments

1

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

3 Comments

For c24-green it works, but how have i to define the regex if it should also work for c24-blue? Than it returns only @c24-blue: #fff;
You mean you want to make it generic or just for c24-green and c24-blue?
At least it should be generic.

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.