Jump to content

BBcode [color]


smiles

Recommended Posts

Hi ! I have a problem with BBcode like thisHere is I am using

$patterns[] = '@\[color=(.*)\](.*)\[/color\]@'; // Matches [color=COLOR]TEXT[/color]$replaces[] = '<font color="$1">$2</font>';

Now it only right when I type

 [color=blue]test[/color]

Not true for all the rest case like:1.

[color=blue]test[/color]

2.

[color=blue]test[/color]

...Could you show me the way to solve it !Thanks :)

Link to comment
Share on other sites

Your link is really nice. I know some about it.So the problem is (.*) just only match any single character with any repeated times, but it ignores newline character ?Try many cases but not find out the good match for it :) Any ideas ?

Link to comment
Share on other sites

My first instinct says

"/\[color\=(.*)](\w+\s?)\[\/color\]/i","<font color=\"$1\">$2</font>"

SHOULD work.. but then again im no expert with regex either.

Link to comment
Share on other sites

Hi ! I use regex for url so when you type http://..... in your post, it auto change to url instead using [code]&#91;url&#93;http&#58;//........&#91;/url&#93;[/code]but i also allow people to embed code for video, audio using embed tag, it also has url (for video, audio) and it creates confliction :) How can I fix it ? (regex continue ?)Thanks !!!
Link to comment
Share on other sites

Hi ! Like this, I am using URL match regex so people don't need to use

[url]link here[/url]

But suppose in the post, they also post the video embed code like this

<object width="425" height="350"><param name="movie" value="http://www.youtube.com/v/cRAMkZxqudg"></param><param name="wmode" value="transparent"></param><embed src="http://www.youtube.com/v/cRAMkZxqudg" type="application/x-shockwave-flash" wmode="transparent" width="425" height="350"></embed></object>

When they click Submit, the post will be checked for URL pattern, the problem starts because it also check URL match in the embed code and replace them as a link ?How can I solve it :)

Link to comment
Share on other sites

Thanks justsomeguy !!!I got your idea and maybe here is the last, anyway it works well to me now :)

$patterns[] = '@((\s|\\\n)((www|http)(\W+\S+[^).,:;?\]\} \r\n$]+)))@'; $replaces[] = ' <a href="$3" target="_blank">$3</a> ';$a_answer = preg_replace($patterns, $replaces, $a_answer);

Link to comment
Share on other sites

You could, but you would have to know which color to search for. So you could maybe search for "black", "white", "red", etc, but if you use a regular expression you can replace any color[/i]"] with <color="any color">, you can just replace the pattern but use the same original value.I'm sure someone will stop by to mention that <color> is not an HTML element, but that's not the point. The point is that with str_replace you can only look for a specific string, with a regular expression you can find something that matches a certain pattern (like xxx[/i]"], whatever xxx is).That being said, if you are only looking for a specific string and not necessarily a pattern, using str_replace is faster then using regular expressions. Regular expressions are powerful but the regular expression engine involves some overhead that makes it slower for replacing vs. str_replace.

Link to comment
Share on other sites

is <color> even a tag? But you would use preg_replace(or eregi_replace) like so:

 $text = $_POST['text']; $text = preg_replace("/\[color\=([\#?a-z0-9]+)\]/i","<span style=\"color:$1\">",$text); echo $text;

Should do it... at least by the example you've given.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...