[Site Home] [Forum Home] [Articles] [File DB] [News Archives]

IceTeks Articles -> Regular expressions and all things nice


(View original topic)


unokpasabaxaki - Mar-24-2008 server time
I also had a "blank page" problem, but I've resolved it.
When I saw the function returned an empty string, I checked the code, and after copying outside of the function, I found out that it failed because I had missed the "code" part. I had:
CODE
$post_abridged = chop($post_no_html);
$bbcode_regex = [...]
$post_bbcode_treated = preg_replace($bbcode_regex, $bbcode_replace, $code_treated);

I changed "$code_treated" to "$post_abridged", because I hadn't defined the "$code_treated" variable at missing the "code" part.
So if you find that the script returns a blank page, make sure you haven't missed any step; and if you have, change the variable name.

[Sorry about my bad English: I'm Spanish and I'm not very confident with it]

AdRock - Sep-01-2007 server time
I have worked out why it wouldn't work before by following the article again but I still can't output what is entered in the form. All I get is a blank page

AdRock - Aug-30-2007 server time
I have got the form with the BBcode working fine now after reading the article properly but I am now stuck on converting teh BBcode into HTML.

First thing...is the file supposed to be called bbcode.php like referred to in the first part of the article?

Second thing....is there a complete listing of the bbcode.php? I have gone through the second part and put the code together but when i try and preview what I have done...it just echoes this

QUOTE
function output_post ($post) { //Make safe any html $post_no_html = htmlspecialchars($post); //Make sure there is no whitespace at the end of the message //It's conceivable that the user will start their message with whitespace $post_abridged = chop($post_no_html); //Callback function for preg_replace_callback below function convert_for_html ($matches) { $regex[0] = "["; $regex[1] = "]"; $replace[0] = "["; $replace[1] = "]"; ksort($regex); ksort($replace); $treated = str_replace($regex, $replace, $matches[1]); $output = 'Code:
' . $treated . '
'; return $output; } //Convert code tags $code_treated = preg_replace_callback( "/\[code\](.+?)\[\/code\]/s", "convert_for_html", $post_abridged); //Arrays for the bbCode replacements $bbcode_regex = array(0 => '/\[b\](.+?)\[\/b\]/s', 1 => '/\[i\](.+?)\[\/i\]/s', 2 => '/\[u\](.+?)\[\/u\]/s', 3 => '/\[quote\](.+?)\[\/quote\]/s', 4 => '/\[quote\=(.+?)](.+?)\[\/quote\]/s', 5 => '/\[url\](.+?)\[\/url\]/s', 6 => '/\[url\=(.+?)\](.+?)\[\/url\]/s', 7 => '/\[img\](.+?)\[\/img\]/s', 8 => '/\[color\=(.+?)\](.+?)\[\/color\]/s', 9 => '/\[size\=(.+?)\](.+?)\[\/size\]/s'); $bbcode_replace = array(0 => '$1', 1 => '$1', 2 => '$1', 3 => 'Quote:
$1
', 4 => '$1 said:
$2
', 5 => '$1', 6 => '$2', 7 => '', 8 => '$2', 9 => '$2'); ksort($bbcode_regex); ksort($bbcode_replace); //preg_replace to convert all remaining bbCode tags $post_bbcode_treated = preg_replace($bbcode_regex, $bbcode_replace, $code_treated); //Convert new lines to
$post_with_br = nl2br($post_bbcode_treated); echo $post_with_br; };


How do i get this working?

Dragon - Nov-10-2005 server time
How about something like this?

Simple to use, wherever a forum like this will allow such code to be used...

BBCode
HTML code
XHTML code


Im using BBCode, for this post, with the little program I posted in first sentence. wink.gif

Red Squirrel - Oct-18-2005 server time
Hey sorry to the person who just posted and got hit with the spam bot. really not sure why it snapped. Must be the word compliant, thats often in spam.

Feel free to register as the spam bot validation wears off after some posts.

MikeDB - Oct-16-2005 server time
Yes yes I dont nkow why im even answering to this post but... Yes.

Red Squirrel - Oct-16-2005 server time
Yes Xhtml is great if you can be diciplined to actually code that strictly, but in my opinion it's too much, just plain normal W3C is good enough for me. em320.gif

Ladytron - Oct-16-2005 server time
You're wrong. Being W3C compliant implies to be a lot more accessible to many other people, like blind people and many others. It also reduces the bandwith used to load web document wink.gif But that's not the right place to talk about this.

We can do simpler for the bbCode tags. Here's an example I do use in a pre-version and bbCode creators could (or should) also use :
  • [b][/b] gives <strong></strong>
  • [i][/i] gives <em></em>
  • [u][/u] gives <span class="underline"></span>
  • text gives <a href="thing.com" title="">text<a/>
Of course, a supply CSS sheet has to be created, but that's worth the effort ! It does not increase any DB size, as all 'tags' are stored either in multiple files (javascript + php, in case of bbCode) or in a single text file that could be parsed.

Anyway (as I don't want to be considered as a troller or a spammer), the method proposed in this article is a good one and is the simplest approach to do bbCode smile.gif

Big up ! wink.gif

Red Squirrel - Oct-15-2005 server time
Yeah but trying to do anything strictly xhtml compliant is nearly impossible and has no benifits, usually just sticking to W3C html 5.02 (or whatever that number is:P) should almost guarantee cross browser compatability. Unless you want a really crappy site <u> <b> <i> are rather useful. You can always use css but then that increases the file size.

ex: <font style="font-weight:bold">hi</font>

vs

<b>hi</b>

Even with a class it's longer:

<font class="b">hi</font>

Though it would be possible to get bbcode to produce this, but it would only increase the file size of the DB.

Ladytron - Oct-15-2005 server time
Hmmm ... This is a really nice tutorial on BBCode-style wink.gif
Therefore, BBCode and its common clones (like PunBB and many other) do suffer from several big problems :
  • The output code is not really XHTML compliant : using <b>,<i> or <u> is NOT HTML/XHTML Strict valid ! pissed.gif
  • Modifying or adding styles needs LOTS of work, too much for the end-user who would add his own styles:eek:
That's why I'm working on a system more flexible and powerful than BBCode (mail me at gaeldir@wanadoo.fr for more details).

Anyway, this tutorial is really helpful ... Especially the part on Regular Expressions ! smile.gif

Fergus Gibson - Jul-27-2005 server time
Jonathan, thanks for this write-up. I'm developing a backdoor to phpbb2 for reasons that might be too involved and boring for the explanation, and I was going about implementing BBCode in a faster but more tedious (for me) way. Your method works great. Way less effort needed on my part. smile.gif

Just a note to others who might try applying this code to phpbb2, the BBCode generated by phpbb2 is a little different. It likes to make tags like [tag:2fb54ad5ee] rather than [tag]. I have no idea what the point of that is since it doesn't seem to do anything, but I had to rewrite nearly all the regular expressions to match the second part (adding as necessary: :\w{10} to the opening and closing tags).

Also for use with phpbb2, I recommend removing the htmlspecialchars(). phpbb2 does this itself and so all you end up doing in this specific case is turning:

CODE
&amp;


into

CODE
&amp;amp;


And for tayfun, easy, use create_function().

CODE
$code_treated = preg_replace_callback(
 "/\[code\](.+?)\[\/code\]/s",
 create_function(
 '$matches',
 '$regex[0] = "[";
 $regex[1] = "]";
 $replace[0] = "[";
 $replace[1] = "]";
 ksort($regex);
 ksort($replace);
 $treated = str_replace($regex, $replace, $matches[1]);
 $output = "<table class=\"code\"><tr><td>Code:</td></tr><tr><td class=\"code_box\">$treated</td></tr></table>";
 return $output;'),
 $post_abridged);


I also believe you should remove the two ksort() calls. They do nothing but waste server CPU cycles in this function. In fact, I removed every single ksort() call in the entire function.

If you'd like to see my mostly done conversion of Jonathan's code to phpbb2, visit this link:

http://www.rafb.net/paste/results/RWY4zu68.html

I'm not sure how long rafb.net keeps things around, but hopefully it'll still be there for the interested when they look. smile.gif


Streety - Jul-05-2005 server time
Thanks for taking the time to read the article kryptx. You're absolutely right. I maintain linebreaks in the cascade stylesheet by setting whitespace as pre:

CODE
.code_box {font-weight:normal;
 font-family: Courier;
 white-space:pre;
 background-color:#00FFFF;
 border-style: solid;
 border-width: 1px;
 border-color: #959595;
 text-indent: 0px;
}


I also apply the nl2br function to the whole post which adds extra line breaks into the code tags.

The best way of dealing with it is probably just deleting whitespace:pre from the stylesheet. Thanks for picking up on the error! smile.gif

hi tayfun, I can't say I've ever seen that error before. I thought at first you may have copied the function into your script twice but on testing this I don't get an error.

You could try using function_exists and before declaring the function and see what it comes back with. Not much else I can suggest I'm afraid.

kryptx - Jul-05-2005 server time
I looked at your code, and it doesn't seem to account for the fact that text within the CODE tags will contain line breaks. Thus, any CODE will be shown as double-spaced.

Just an FYI.

tayfun - Jul-05-2005 server time
Hi! Thank you for the excellent script and tutorial. I have implemented it on my site however I'm getting a problem where it can't call to the function more than once:

Fatal error: Cannot redeclare convert_for_html() (previously declared in C:\apachefriends\xampp\htdocs\fzero\user_auth_fns.php:226) in C:\apachefriends\xampp\htdocs\fzero\user_auth_fns.php on line 226

any ideas??!

Red Squirrel - Apr-23-2005 server time
This is the second article looking at how to allow your visitors to post content using bbCode. While the previous article looked at the client-side processing this article will deal with how to treat bbCode formatted text once it reaches your server.

http://www.iceteks.com/articles.php/javascript2/1

(Showing 50 last posts, newest on top)