smiles Posted September 22, 2007 Share Posted September 22, 2007 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 More sharing options...
zppblood Posted September 22, 2007 Share Posted September 22, 2007 Regular Expressions Cheat Sheethttp://www.ilovejackdaniels.com/regular_ex...cheat_sheet.png Link to comment Share on other sites More sharing options...
smiles Posted September 23, 2007 Author Share Posted September 23, 2007 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 More sharing options...
smiles Posted September 23, 2007 Author Share Posted September 23, 2007 Temporarily, use it to solve $patterns[] = '@\[color=(.*)\]@'; // Matches [color=COLOR]TEXT[/color]$replaces[] = '<font color="$1">';$patterns[] = '@\[/color\]@';$replaces[] = '</font>'; Link to comment Share on other sites More sharing options...
jhecht Posted September 24, 2007 Share Posted September 24, 2007 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 More sharing options...
justsomeguy Posted September 24, 2007 Share Posted September 24, 2007 If you're using PCRE, you can use the s modifier to make the dot include all characters.http://www.php.net/manual/en/reference.pcr...n.modifiers.php Link to comment Share on other sites More sharing options...
smiles Posted September 26, 2007 Author Share Posted September 26, 2007 Hi ! I use regex for url so when you type http://..... in your post, it auto change to url instead using [code][url]http://........[/url][/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 More sharing options...
justsomeguy Posted September 26, 2007 Share Posted September 26, 2007 Is this a completely different question then the original? Is the original problem solved? What's your new question, what does it look like when people embed media? Link to comment Share on other sites More sharing options...
smiles Posted September 26, 2007 Author Share Posted September 26, 2007 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 More sharing options...
justsomeguy Posted September 27, 2007 Share Posted September 27, 2007 Make sure there aren't quotes around the URL, if there are quotes around the URL don't replace it. Link to comment Share on other sites More sharing options...
smiles Posted September 27, 2007 Author Share Posted September 27, 2007 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 More sharing options...
Mememe Posted October 2, 2007 Share Posted October 2, 2007 Don't lash out at me or anything. I'm kind of new to PHP, but for color, couldn't you use the str_replace, and replace all , to <color="black">, for example? Link to comment Share on other sites More sharing options...
JamesB Posted October 2, 2007 Share Posted October 2, 2007 couldn't you use the str_replace, and replace all , to <color="black">, for example? I was thinking the exact same thing, but Im also new to PHP. Link to comment Share on other sites More sharing options...
justsomeguy Posted October 2, 2007 Share Posted October 2, 2007 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 More sharing options...
jhecht Posted October 3, 2007 Share Posted October 3, 2007 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 More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.