Mark White
Mark White

Reputation: 46

PHP preg_replace square brackets

How could I replace some markup in this format:

[a href="/my_page" style="font-size: 13px"]click me[/a]

to

<a href="/my_page" style="font-size: 13px">click me</a>

using preg_replace()?

I will need to allow for more attributes as well.

Upvotes: 2

Views: 2604

Answers (1)

xdazz
xdazz

Reputation: 160883

$s = '[a href="/my_page" style="font-size: 13px"]click me[/a]';
$ret = preg_replace('~\[([^\[\]]+)\]([^\[\]]++)\[/([^\[\]]++)\]~', '<\1>\2</\3>', $s);

Upvotes: 2

Related Questions