0

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.

4
  • 3
    change (\S+?) to (.+?) -- but you will have problems with nested or overlapping tags. Why not simply replace [white-text] with <font color=white> and [/white-text] with </font> independently of each other to avoid nesting/overlap problems? Commented Nov 24, 2012 at 1:33
  • And, incidentally, the font element has been deprecated. You should probably be using span elements with classes (or inline styles, if you really have to). Commented Nov 24, 2012 at 1:38
  • Vladr, I didn't made it independently, cause I thought it wasn't good practice, but I will do that now. Just confirm it for me, it would look like this: $string = preg_replace("/[white-text]/","<font color=white>", $string); - RIGHT ? Commented Nov 24, 2012 at 2:19
  • Hello David Thomas, I'm using span classes, I just put the example like this to be easier to understand. Commented Nov 24, 2012 at 2:20

1 Answer 1

2

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>"
Sign up to request clarification or add additional context in comments.

1 Comment

Oh, that seems easy and fine ... I'm testing it right now - THANK YOU!

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.