I've got my site in a fairly reasonable state now. I'm parsing the BBC Technology, Slashdot or GameDev.Net RSS feeds (randomly selected) for a bit more substance on the page, I've got a very simple thumbnailing script for my Image Of The Day gallery, and I can read posts from my forum. The hardest part of it all was parsing the damn bbcode. Since I've never used regex before, I really didn't have a clue what I was doing. But I finally got it working, with help from Gyzmo, Pouya, and johnb. I can parse: url (both blah and blah.com), img, b, i, u, list (and
- ,
- ), quote, and code.
The code is a bit... funky, but it works well enough for me. Here's what I have:$search = array( '/(\)(.+)(\[\/url\])/',<br> '/(\[url\])(.+)(\[\/url\])/',<br> '/(\[img:[<span class="cpp-number">0</span>-9a-fA-F]{<span class="cpp-number">10</span>}\])(.+)(\[\/img:[<span class="cpp-number">0</span>-9a-fA-F]{<span class="cpp-number">10</span>}\])/',<br> '/(\[[Bb]:[<span class="cpp-number">0</span>-9a-fA-F]{<span class="cpp-number">10</span>}\])(.+)(\[\/[Bb]:[<span class="cpp-number">0</span>-9a-fA-F]{<span class="cpp-number">10</span>}\])/',<br> '/(\[[Ii]:[<span class="cpp-number">0</span>-9a-fA-F]{<span class="cpp-number">10</span>}\])(.+)(\[\/[Ii]:[<span class="cpp-number">0</span>-9a-fA-F]{<span class="cpp-number">10</span>}\])/',<br> '/(\[[Uu]:[<span class="cpp-number">0</span>-9a-fA-F]{<span class="cpp-number">10</span>}\])(.+)(\[\/[Uu]:[<span class="cpp-number">0</span>-9a-fA-F]{<span class="cpp-number">10</span>}\])/',<br> '/(\[list:[<span class="cpp-number">0</span>-9a-fA-F]{<span class="cpp-number">10</span>}\])/',<br> '/(\[list=<span class="cpp-number">1</span>:[<span class="cpp-number">0</span>-9a-fA-F]{<span class="cpp-number">10</span>}\])/',<br> '/(\[\/list:u:[<span class="cpp-number">0</span>-9a-fA-F]{<span class="cpp-number">10</span>}\])/',<br> '/(\[\/list:o:[<span class="cpp-number">0</span>-9a-fA-F]{<span class="cpp-number">10</span>}\])/',<br> '/(\[\*:[<span class="cpp-number">0</span>-9a-fA-F]{<span class="cpp-number">10</span>}\])(.+?)/',<br> '/(\[quote:[<span class="cpp-number">0</span>-9a-fA-F]{<span class="cpp-number">10</span>}=\<span class="cpp-literal">"(.+)\"</span>\])(.+)(\[\/quote:[<span class="cpp-number">0</span>-9a-fA-F]{<span class="cpp-number">10</span>}\])/',<br> '/(\[quote:[<span class="cpp-number">0</span>-9a-fA-F]{<span class="cpp-number">10</span>}\])/',<br> '/(\[\/quote:[<span class="cpp-number">0</span>-9a-fA-F]{<span class="cpp-number">10</span>}\])/',<br> );<br>$replace = array(<br> '<a href=<span class="cpp-literal">"\\2"</span>>\\<span class="cpp-number">4</span></a>',<br> '<a href=<span class="cpp-literal">"\\2"</span>>\\<span class="cpp-number">2</span></a>',<br> '<img src=<span class="cpp-literal">"\\2"</span> alt=<span class="cpp-literal">"Image"</span>/>',<br> '<b>\\<span class="cpp-number">2</span></b>',<br> '<i>\\<span class="cpp-number">2</span></i>',<br> '<span <span class="cpp-keyword">class</span>=<span class="cpp-literal">"underline"</span>>\\<span class="cpp-number">2</span></span>',<br> '<ul>',<br> '<ol>',<br> '</ul>',<br> '</ol>',<br> '<li>\\<span class="cpp-number">2</span></li>',<br> '<table <span class="cpp-keyword">class</span>=<span class="cpp-literal">"quotetable"</span>><tr><td><div <span class="cpp-keyword">class</span>=<span class="cpp-literal">"smalltext"</span>>\\<span class="cpp-number">2</span> wrote:</div>\\<span class="cpp-number">3</span></td></tr></table>',<br> '<table <span class="cpp-keyword">class</span>=<span class="cpp-literal">"quotetable"</span>><tr><td><div <span class="cpp-keyword">class</span>=<span class="cpp-literal">"smalltext"</span>>Quote:</div>',<br> '</td></tr></table>',<br> );<br><br>$post = preg_replace($search,$replace,$row['post_text']);<br>$search = array(<span class="cpp-literal">"<ul>\r\n"</span>,<span class="cpp-literal">"<ol>\r\n"</span>,<span class="cpp-literal">"</li>\r\n"</span>);<br>$replace = array('<ul>','<ol>','</li>');<br>$post = str_replace($search,$replace,$post);<br>$post = preg_replace_callback('/(\[code:<span class="cpp-number">1</span>:[<span class="cpp-number">0</span>-9a-fA-F]{<span class="cpp-number">10</span>}\])(.+)(\[\/code:<span class="cpp-number">1</span>:[<span class="cpp-number">0</span>-9a-fA-F]{<span class="cpp-number">10</span>}\])/s',code_callback,$post);<br>$post = str_replace(<span class="cpp-literal">"\r\n"</span>,<span class="cpp-literal">"<br/>\n"</span>,$post);<br>$post = str_replace('specialtag_newlines_lol',<span class="cpp-literal">"\n"</span>,$post);<br><br></pre></div><!--ENDSCRIPT--><br>First, I replace all the common tags, then I strip newlines from lists. Without that, the page doesn't validate because you end up with <ul><br/>, and you haven't have <br/> tags in a list.<br>Once that's done, I use a callack to parse <code> tags. They need special handling because I use geshi to parse the code inside them into nicely colured text, much like the source tags here on GDNet.<br>Inside the code callback function, I convert special characters back to their normal equivalents (( becomes ( and so on), since phpbb converts those, and geshi will convert them back if it needs. Then whitespace is trimmed, and the code is parsed with geshi. Finally, newlines get converted into a tag, so when I come to convert newlines to br's, I don't end up with <br/> all over my code.<br>Back out of the callback, I convert newlines to <br/> tags, and then put the newlines back into the code blocks.<br><br>Phew.<br>I spent most of yesterday watching Lost, season 1. I started watching it when it was on TV here, but I only saw the first 4 or 5 episodes, then missed about 5 of them and decided it was pointless trying to get back into it, and I'll just get the DVD when it comes out.<br>I've still got the second half of the season to watch tonight (Another 9 hours in total). Why is it that when my MSN name is set to "Steve - Watching Lost (Away)", everyone tries to sodding talk to me? I had 5 people leaving messages, including one who kept saying "Pssssst!" "timmy!" and sending repeated nudges. However, my MSN doesn't bleep or anything when people send me messages/nudges when media player is running fullscreen...<div> </div>
- ,