1

Got text a little like this coming out of a database

[color=#BF0000]This is just a very simple test. 
Cheers Fred[/color]

Now this text could have newlines anywhere in it between the [color...] and [/color].

Been trying to use this regex within preg_replace, this is the pattern

\[color=(#[0-9a-f]{3}|#[0-9a-f]{6}|[a-z\-]+)\](.*?)\[\/color\]/i

and gets replaced with

<span style=\"color:$1\">$2</span>

All works great if there are no newlines in the text. I know I need to make changes around (.*?) but no idea what, can anyone help with this?

1 Answer 1

2

Use s modifier to make dot match newlines:

'~\[color=(#[0-9a-f]{3}|#[0-9a-f]{6}|[a-z-]+)\](.*?)\[/color\]~is'

Also you can use an alternate regex delimiter to avoid escaping forward slash.

Sign up to request clarification or add additional context in comments.

1 Comment

Groan as simple at that, I'd tried all ways but that, thanks

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.