Regular expressions and all things nice

Anything technology related that does not fit in other categories
Locked
User avatar
Red Squirrel
Posts: 29193
Joined: Wed Dec 18, 2002 12:14 am
Location: Northern Ontario
Contact:

Regular expressions and all things nice

Post by Red Squirrel »

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

Archived topic from Iceteks, old topic ID:3266, old post ID:26505
Honk if you love Jesus, text if you want to meet Him!
Streety
Posts: 182
Joined: Mon Aug 16, 2004 7:12 pm

Regular expressions and all things nice

Post by Streety »

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: Select all

.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;
}[code]  

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! :)

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 [url=http://www.php.net/function_exists]function_exists[/url] and before declaring the function and see what it comes back with.  Not much else I can suggest I'm afraid. 

[color=#888888][size=85]Archived topic from Iceteks,  old topic ID:3266, old post ID:27946[/size][/color]
Image
User avatar
Red Squirrel
Posts: 29193
Joined: Wed Dec 18, 2002 12:14 am
Location: Northern Ontario
Contact:

Regular expressions and all things nice

Post by Red Squirrel »

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.

Archived topic from Iceteks, old topic ID:3266, old post ID:31341
Honk if you love Jesus, text if you want to meet Him!
User avatar
Red Squirrel
Posts: 29193
Joined: Wed Dec 18, 2002 12:14 am
Location: Northern Ontario
Contact:

Regular expressions and all things nice

Post by Red Squirrel »

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. :D

Archived topic from Iceteks, old topic ID:3266, old post ID:31402
Honk if you love Jesus, text if you want to meet Him!
User avatar
MikeDB
Posts: 1226
Joined: Fri May 13, 2005 3:09 pm

Regular expressions and all things nice

Post by MikeDB »

Yes yes I dont nkow why im even answering to this post but... Yes.

Archived topic from Iceteks, old topic ID:3266, old post ID:31404
User avatar
Red Squirrel
Posts: 29193
Joined: Wed Dec 18, 2002 12:14 am
Location: Northern Ontario
Contact:

Regular expressions and all things nice

Post by Red Squirrel »

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.

Archived topic from Iceteks, old topic ID:3266, old post ID:31493
Honk if you love Jesus, text if you want to meet Him!
Anonymous

Regular expressions and all things nice

Post by Anonymous »

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
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: Select all

(.+?)
/s", "convert_for_html", $post_abridged); //Arrays for the bbCode replacements $bbcode_regex = array(0 => '/(.+?)/s', 1 => '/(.+?)/s', 2 => '/(.+?)/s', 3 => '/
(.+?)
/s', 4 => '/
(.+?) wrote:(.+?)
/s', 5 => '/(.+?)/s', 6 => '/(.+?)/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?

Archived topic from Iceteks, old topic ID:3266, old post ID:38237
Anonymous

Regular expressions and all things nice

Post by Anonymous »

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

Archived topic from Iceteks, old topic ID:3266, old post ID:38239
Anonymous

Regular expressions and all things nice

Post by Anonymous »

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: Select all

$post_abridged = chop($post_no_html);
$bbcode_regex = [...]
$post_bbcode_treated = preg_replace($bbcode_regex, $bbcode_replace, $code_treated);[code]
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]

[color=#888888][size=85]Archived topic from Iceteks,  old topic ID:3266, old post ID:38725[/size][/color]
Locked