Genesis
Genesis

Reputation: 1347

Formatting text with regex

I have two functions to format the text of my notices.

1. Converts [white-text][/white-text] into <font color=white></font>

$string = preg_replace("/\[white-text\](\S+?)\[\/white-text\]/si","<font color=white>\\1</font>", $string);

2. Converts [url][/url] into <a href></a>

$string = preg_replace("/\[url\](\S+?)\[\/url\]/si","<a href=\"http://\\1\" target=\"_blank\">\\1</a>", $string);

Problems:
WHITE-TEXT - It only changes the color if the phrase has only ONE word.
URL - It works fine, but I would like to be able to write anything in the readable part of the URL.

Upvotes: 0

Views: 177

Answers (1)

Paul S.
Paul S.

Reputation: 66334

URL - It works fine, but I would like to be able to write anything in the readable part of the URL.

Make the URL code have the form [url=href]description[/url], you can then use this simple RegExp

"/\[url=([^\]]*)\](.+?)\[\/url\]/si"
"<a href=\"http://\\1\" target=\"_blank\">\\2</a>"

Upvotes: 2

Related Questions