HaLo2FrEeEk Posted July 29, 2007 Share Posted July 29, 2007 Ok, phpbb, at least, puts a unique ID number into each bbcode tag a user posts, for example, if I were to post bold text and look at the raw code in the database, it would look like this: [b:9f0e836e4c]bold text[/b:9f0e836e4c] Now, it's simple enough to remove those numbers using this regex: [b:[^\]]+] What's really got me stumped, though, is how to do this when the bbcode has attributes, such as size and a quote: Test quote Would end up being: Test quote And: Test size turns into: Test size What I need to know is how I can use one regex to handle both the opening, AND the closing tags for these, and still keeping the attribute value (so saving it to a backreference). I used this one for size, but it only did the opening tag, and not the closing one: preg_replace("[size(=[0-9]{1,2}):[^\]]+]", "size$1", $text); The thought was that if the attribute was there (i.e., an opening tag) then it would save the backreference and replace it, if it's not there, it will ignore it. It doesn't work though, and it only replaces the opening tag, forcing me to have another regex to handle the closing tags. What can I do to fix this? Thank you. Quote Link to comment Share on other sites More sharing options...
HaLo2FrEeEk Posted July 29, 2007 Author Share Posted July 29, 2007 Ok, I got the size one, I feel dumb, all I needed was to add a question mark, to make the part getting the attribute optional, but it won't work for the quote tag, no matter what I try, I can't get this to work. This regex: ]+(=[.]*)] Turns: Test[/quote:bcb8669df7] Into: [quote="HaLo2FrEeEk"]Test[/quote:bcb8669df7] So it works halfway. I tried adding a question mark, and this regex: ]+(=[.]*)?] Turns: Test[/quote:bcb8669df7] Into: [quote]Test[/quote] No attributes. I'm about to pull my hairs out...and I need them, but this is driving me insane. Quote Link to comment Share on other sites More sharing options...
HaLo2FrEeEk Posted July 29, 2007 Author Share Posted July 29, 2007 I still need help. Quote Link to comment Share on other sites More sharing options...
HaLo2FrEeEk Posted July 30, 2007 Author Share Posted July 30, 2007 bump. Quote Link to comment Share on other sites More sharing options...
effigy Posted July 30, 2007 Share Posted July 30, 2007 Do you need nesting capabilities? <pre> <?php $tests = array( '[b:9f0e836e4c]bold text[/b:9f0e836e4c]', '[quote:9f0e836e4c="HaLo2FrEeEk"]Test quote[/quote]', '[size=14:9f0e836e4c]Test size[/size]' ); foreach ($tests as $test) { echo $test, ' => '; echo preg_replace('# \[(b|quote|size=\d+):[^]=]+(=".+?")?\] (.*?) \[/([^:]+)(?:[^]]+)?\] #x', '[\1\2]\3[/\4]', $test); echo '<br>'; } ?> </pre> Quote Link to comment Share on other sites More sharing options...
HaLo2FrEeEk Posted August 2, 2007 Author Share Posted August 2, 2007 Holy!... How in bloody hell did you do that? Thank you, could you possibly tell me how it works, so I can try to do it myself in the future. Also, could you tell me if I have the below regex right to turn this: [url=http://claninfectionist.com/]Clan Infectionist[/url] into this: <a href="http://claninfectionist.com/">Clan Infectionist</a> REGEX: \[url=(.+?)\](.+?)\[\/url\] Will that work? If so, is there a better way, if not, what can I use? And thank you again. EDIT: I'd also like to know how I can turn: [url]http://claninfectionist.com[/url] into: <a href="http://claninfectionist.com">http://claninfectionist.com</a> If it's not too much trouble. Quote Link to comment Share on other sites More sharing options...
effigy Posted August 2, 2007 Share Posted August 2, 2007 ### Inside square braces find... \[ (b|quote|size=\d+) ### Either one of these. : ### A colon. [^]=]+ ### Anything that's not an end square brace or an equals sign. (=".+?")? ### An optional equals sign follow by quoted text. \] (.*?) ### Optional content between the begin and end tags. ### Inside square braces find... \[ / ### A forward slash. ([^:]+)(?:[^]]+)? ### Optional non-colon content, followed by a colon and non-end-square-brace content. \] Based on this, show me what you've tried for the other ones you've posted. Quote Link to comment Share on other sites More sharing options...
HaLo2FrEeEk Posted August 3, 2007 Author Share Posted August 3, 2007 Ok, lemme try. preg_replace("#\[url((=.+?)?|\])(.+?)[\/url]#", "<a href=\"$1\">$2</a>", $string); I don't know...something seems wrong with that. EDIT: This is the one I tried, it needs to be able to handle both [url=URL]Something[/url] and [url]URL[/url] : preg_replace("|\[url[=]?(.+?)?\](.+?)\[\/url\]|", "<a href=\"$1\">$2</a>", $string); It doesn't work for the second one [url]URL[/url] it only works for the first one. Quote Link to comment Share on other sites More sharing options...
effigy Posted August 3, 2007 Share Posted August 3, 2007 <pre> <?php $tests = array( '[url=http://claninfectionist.com/]Clan Infectionist[/url]', '[url]http://claninfectionist.com[/url]', ); foreach ($tests as $test) { echo $test, ' => '; echo htmlspecialchars( preg_replace("# \[url (?:=([^]]+))? \] (.+?) \[/url\] #xe", '"<a href=\"" . ("\1" == "" ? "\2" : "\1") . "\">\2</a>"', $test) ); echo '<br>'; } ?> </pre> Quote Link to comment Share on other sites More sharing options...
HaLo2FrEeEk Posted August 3, 2007 Author Share Posted August 3, 2007 You are a bloody genius, I love you. Thank you. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.