By: martijn
Date: 2006-09-03
Time: 14:31
|
Using formating functions
Hi,
I'm coding an CMS for my website but run into some problems.
What I would like to do is enabling a BBCode parsing function for text on my newsposts.
I already tried to pass the function inside the MergeBlock (wich ofcourse didn't work), then tried the onformat and ondata options, wich wouldn't work either.
The function is inside a class but even outside the class it won't work.
Can someone help me out on this?
Here's some off my code:
index.php:
<?php
include_once('config.php');
//Load the template
$tmpl->LoadTemplate('template/home.html');
//Database data
$data1 = $tmpl->MergeBlock('com','mysql', "SELECT COUNT(articleid) AS num FROM articles AS a LEFT JOIN comments AS b ON (a.id=b.articleid) LIMIT 0,5");
$data2 = $tmpl->MergeBlock('article', 'mysql', "SELECT * FROM articles WHERE published = 1 WHERE seftitle = ".$_GET['title']." ORDER BY id DESC");
$data3 = $tmpl->MergeBlock('files','mysql',"SELECT * FROM `files` ORDER BY id DESC LIMIT 0,5");
$data4 = $tmpl->MergeBlock('latest','mysql',"SELECT * FROM `articles` WHERE published = 1 ORDER BY id DESC LIMIT 0,5");
//Render
$tmpl->Show() ;
?> |
home.html:
[onload;file=template/overall_header.html]
<div class="newsitem">
<h2>
<a href="[var.script_path]/news/view/[latest.id;].html" class="news"><img src="./images/article.gif" alt=""/> [latest.title;htmlconv=no;]</a>
</h2>
<span class="news">[latest.text;htmlconv=no;onformat=portal.BBCode;block=div]</span>
<div class="newsline">Posted on [latest.date;frm='ddd dd mmm'] by [latest.submitter] | Comments ([com.num;])</div>
</div>
[onload;file=template/overall_footer.html]
|
And here is in example off an newspost that I want to format using it:
I've been learning to program in [b]OOP[/b] for a little while now and it turns out to be very easy to do.
Basically you bundle all your functions in a class to assign them all for use in a main variable.
More updates soon! :) |
The word "[b]OOP[/b]" should have been formatted bold.
And last but not least, here is the function:
function BBCode($message) {
$message = preg_replace('/(?:\[url=([^\]\s]+)\]([^\[\]]+)\[\/url\])/i', '<a href="$1">$2</a>', $message);
$message = preg_replace('/(?:\[[e]?mail=([^\]\s]+)\]([^\[\]]+)\[\/[e]?mail\])/i', '<a href="mailto:\\1">\\2</a>', $message);
$message = preg_replace('/(?:\[img=([^\]\s]+)\]([^\[\]]+)\[\/img\])/i', '<img src="\\1" title="\\2" alt="\\2" style="border:0;" />', $message);
$message = preg_replace('/(?:\[(?:b|strong)\]([^\[\]]+)\[\/(?:b|strong)\])/i', '<strong>\\1</strong>', $message);
$message = preg_replace('/(?:\[(?:i|em)\]([^\[\]]+)\[\/(?:i|em)\])/i', '<em>\\1</em>', $message);
$message = preg_replace('/(?:\[u\]([^\[\]]+)\[\/u\])/i', '<span style="text-decoration:underline;">\\1</span>', $message);
$message = preg_replace('/(?:\[(?:s|strike)\]([^\[\]]+)\[\/(?:s|strike)\])/i', '<span style="text-decoration:line-through;">\\1</span>', $message); // added [s] functionality, not tested!
$message = preg_replace('/(?:\[img\]([^\[\]\s]+)\[\/img\])/i', '<img src="\\1" alt="" style="border:0;" />', $message);
$message = preg_replace('/(?:\[url\]([^\[\]\s]+)\[\/url\])/i', '<a href="\\1">\\1</a>', $message);
$message = preg_replace('/(?:\[[e]?mail\]([^\[\]\s]+)\[\/[e]?mail\])/i', '<a href="mailto:\\1">\\1</a>', $message);
$message = preg_replace('/(?:\[(center|left|right|justify)\]([^\[\]]+)\[\/(center|left|right|justify)\])/i', '<div style="text-align:\\1">\\2</div>', $message); // not tested!
$message = str_replace(array("\r\n", "\n", "\r"), '<br />', $message);
return $message;
} |
|
By: martijn
Date: 2006-09-03
Time: 14:33
|
Re: Using formating functions
To add something to the above post:
I hope you guys can help me out.
Thanks in advance!
|
By: martijn
Date: 2006-09-03
Time: 14:55
|
Re: Using formating functions
Just found out that this should've been in the programming with TBS forum. Can it be moved?
|
By: Skrol29
Date: 2006-09-03
Time: 20:17
|
Re: Using formating functions
Hi Martin,
The only thing that is missing to have your snippet working properly is to use the correct syntaxe for the onformat TBS function.
It you code your class like the following, then the template works (I've tested it) :
class portal {
function BBCode($FieldName,&$CurrVal) {
$Value = BBCode($CurrVal);
}
}
|
You can also change you BBCode() function to fit with TBS syntax.
By the way, may I publish yourBBCode() source in the Tip & Tricks forum ?
|
By: martijn
Date: 2006-09-04
Time: 12:58
|
Re: Using formating functions
Thanks alot for your reply!
I'll test it out right away.
For publishing the BBCode function, it's fine with me, but I got it from someone else myself. I'll ask that person if he's ok with it first, then I'll reply back.
Thanks for the help :)
|
By: martijn
Date: 2006-09-04
Time: 13:19
|
Re: Using formating functions
I'm a bit puzzled on how to integrate your code with my code. You can't use a the same function in the function itself.
My code:
class portal{
...
function BBCode($FieldName,&$CurrVal) {
$message = preg_replace('/(?:\[url=([^\]\s]+)\]([^\[\]]+)\[\/url\])/i', '<a href="$1">$2</a>', $message);
$message = preg_replace('/(?:\[[e]?mail=([^\]\s]+)\]([^\[\]]+)\[\/[e]?mail\])/i', '<a href="mailto:\\1">\\2</a>', $message);
$message = preg_replace('/(?:\[img=([^\]\s]+)\]([^\[\]]+)\[\/img\])/i', '<img src="\\1" title="\\2" alt="\\2" style="border:0;" />', $message);
$message = preg_replace('/(?:\[(?:b|strong)\]([^\[\]]+)\[\/(?:b|strong)\])/i', '<strong>\\1</strong>', $message);
$message = preg_replace('/(?:\[(?:i|em)\]([^\[\]]+)\[\/(?:i|em)\])/i', '<em>\\1</em>', $message);
$message = preg_replace('/(?:\[u\]([^\[\]]+)\[\/u\])/i', '<span style="text-decoration:underline;">\\1</span>', $message);
$message = preg_replace('/(?:\[(?:s|strike)\]([^\[\]]+)\[\/(?:s|strike)\])/i', '<span style="text-decoration:line-through;">\\1</span>', $message); // added [s] functionality, not tested!
$message = preg_replace('/(?:\[img\]([^\[\]\s]+)\[\/img\])/i', '<img src="\\1" alt="" style="border:0;" />', $message);
$message = preg_replace('/(?:\[url\]([^\[\]\s]+)\[\/url\])/i', '<a href="\\1">\\1</a>', $message);
$message = preg_replace('/(?:\[[e]?mail\]([^\[\]\s]+)\[\/[e]?mail\])/i', '<a href="mailto:\\1">\\1</a>', $message);
$message = preg_replace('/(?:\[(center|left|right|justify)\]([^\[\]]+)\[\/(center|left|right|justify)\])/i', '<div style="text-align:\\1">\\2</div>', $message); // not tested!
$message = str_replace(array("\r\n", "\n", "\r"), '<br />', $message);
$Value = BBCode($CurrVal); //"Fatal error: Call to undefined function BBCode() in E:\wamp\www\ubb\scripts\class.portal.php on line 84"
return $message;
}
...
}
|
|
By: Skrol29
Date: 2006-09-04
Time: 19:02
|
Re: Using formating functions
Hi Martijn,
You should copy/past with understanding. You can see that in your new snippet, variable $message is calculated but never used in the process.
I must say, my snippet has obviously an error too because $Value is never used neither.
The point is that the TBS syntax needs a function that updates its seconde argument (named $CurrVal here) instead of returning a value. This is possible because this argument is passed by reference (the & before the argument in the declaration).
So your code should be:
class portal {
function BBCode($FieldName,&$CurrVal) {
$CurrVal = preg_replace(... , ... , $CurrVal);
$CurrVal = preg_replace(... , ... , $CurrVal);
...
// without return !
}
}
|
|
By: martijn
Date: 2006-09-05
Time: 07:44
|
Re: Using formating functions
Thanks a lot for the help!
It works now!
You can post that code in the tips and tricks forum if you want, it's okay :)
|
By: martijn
Date: 2006-09-05
Time: 07:57
|
Another question/problem
I got another question aswell now.
I'd like to display for each newspost on the home page how many comments there are for an article.
There are many methods wich I tried, but I end up with it counting either 1,0 or the total number of comments that is in the database and displaying it with each post instead of the actual post itself.
Example of what it should be like
------
post 1
comments in database: 0
------
post 2
comments in database: 2
------
When I try to use COUNT() in the query it counts the total number of comments, wich in this case are belonging to only the second post. It is outputted however on both posts as 2, while only the second post should display 2 and the first one 0.
My tables layout is like this:
A table called 'articles' with an row called 'id' and a table called 'comments' with two important rows called 'id' and 'articleid'. The row 'id' from the articles table and the row 'articleid' are related to eachother wich I want to use for counting.
I hope this all makes sense and that there is a solution to it (there should be...)
Thanks in advance!
Martijn
|
By: Skrol29
Date: 2006-09-06
Time: 10:13
|
Re: Another question/problem
Hi Martijn,
If you have MySQL 5, the simpler is to calculate is count in a new column of your query using a subquery. MySQL 4 does not support subqueries.
Otherwise, I suggest that you merge the count using a sub block. See the example in line for understanding TBS subblocks.
|
By: NeverPanic
Date: 2006-09-09
Time: 11:48
|
Re: Another question/problem
OK, and here's the official OK for copying the BBCode if you guys want... because I wrote it ;-)
There are still some lines missing (like [color][font][size][center][left][right][justify]) which I will add later, so maybe you open a new topic and I will update the BBCode once I add these features.
|
By: NeverPanic
Date: 2006-09-09
Time: 11:54
|
Re: Another question/problem
function BBCode($dropthis, &$message) {
$message = preg_replace('/(?:\[url=([^\]\s]+)\]([^\[\]]+)\[\/url\])/i', '<a href="$1">$2</a>', $message);
$message = preg_replace('/(?:\[[e]?mail=([^\]\s]+)\]([^\[\]]+)\[\/[e]?mail\])/i', '<a href="mailto:\\1">\\2</a>', $message);
$message = preg_replace('/(?:\[img=([^\]\s]+)\]([^\[\]]+)\[\/img\])/i', '<img src="\\1" title="\\2" alt="\\2" style="border:0;" />', $message);
$message = preg_replace('/(?:\[(?:b|strong)\]([^\[\]]+)\[\/(?:b|strong)\])/i', '<strong>\\1</strong>', $message);
$message = preg_replace('/(?:\[(?:i|em)\]([^\[\]]+)\[\/(?:i|em)\])/i', '<em>\\1</em>', $message);
$message = preg_replace('/(?:\[u\]([^\[\]]+)\[\/u\])/i', '<span style="text-decoration:underline;">\\1</span>', $message);
$message = preg_replace('/(?:\[(?:s|strike)\]([^\[\]]+)\[\/(?:s|strike)\])/i', '<span style="text-decoration:line-through;">\\1</span>', $message); // added [s] functionality, not tested!
$message = preg_replace('/(?:\[img\]([^\[\]\s]+)\[\/img\])/i', '<img src="\\1" alt="" style="border:0;" />', $message);
$message = preg_replace('/(?:\[url\]([^\[\]\s]+)\[\/url\])/i', '<a href="\\1">\\1</a>', $message);
$message = preg_replace('/(?:\[[e]?mail\]([^\[\]\s]+)\[\/[e]?mail\])/i', '<a href="mailto:\\1">\\1</a>', $message);
$message = preg_replace('/(?:\[(center|left|right|justify)\]([^\[\]]+)\[\/(center|left|right|justify)\])/i', '<div style="text-align:\\1">\\2</div>', $message); // not tested!
$message = str_replace(array("\r\n", "\n", "\r"), '<br />', $message);
return $message;
} |
I'd recommend to modify the function like this if you want to use it with onformat. However, doing this would require the server to parse the BBCode every time the site is loaded. I do parse BBCode on inserting into the database, so it's only processed once.
There is a way to convert HTML back to BBCode (e.g. for editing) and I might write another function for this soon.
|
By: TomH
Date: 2006-09-09
Time: 14:38
|
Re: Using formating functions
Have been working on a CMS as well - so I am very interested in your approach...
If you would be so kind as to explain your thinking about why and how you are using BBCode for your posts, it would be greatly appreciated.
Sincerely,
|
By: NeverPanic
Date: 2006-09-09
Time: 21:12
|
Re: Using formating functions
OK, here we go:
How I am using BBCode: (if this forum supported BBCode, i'd use bold for this headline)
I'm using BBCode in textareas in my guestbook (make sure to include a spam filter!) and in my news comments.
I'm parsing the BBCode --> HTML when submitting a new entry, so the server only processes it once (remember regex is slow!).
I know there is a possibility to convert HTML back to BBCode for editing, I just would have to write some more regexpressions to do that.
I'm not using BBCode to write articles, because I use a rich text editor there... but I'm planning to switch to BBCode, too, because BBCode is easier to integrate with AJAX. You might want to add some admin-only BBCodes like [float:left] or something for images, which would be done with a simple if($login) around the admin regex.
Why I'm using BBCode:
You can make your code more readable using BBCode, i.e. you can emphasize some words that are über important and or you might mark headlines with BBCode.
Why not HTML? For security reasons. (beware of JavaScript cross-site attacks)
|
By: NeverPanic
Date: 2006-09-09
Time: 21:19
|
Re: Another question/problem
Just ask me, I do have a solution for that. Try this query:
SELECT
np_news.id AS id,
np_news.title AS title,
np_news.title_en AS title_en,
np_news.content AS content,
np_news.content_en AS content_en,
np_news.datetime AS datetime,
np_news.category AS category,
np_news.category_en AS category_en,
np_news.author AS author,
np_news.enable_comments AS enable_comments,
COUNT(np_news_comments.id) AS comments
FROM np_news
LEFT JOIN np_news_comments
ON np_news.id = np_news_comments.newsid
WHERE np_news.online='1' AND np_news.id = '%s'
GROUP BY np_news.id
|
|
By: martijn
Date: 2006-09-10
Time: 13:49
|
Re: Another question/problem
Hi NeverPanic,
You didn't show up on GTalk or MSN (at least not when I was on), so I didn't have a chance ask you for permission, sorry about that ;)
I'm still strugling with those comments but I just replied to your email.
|
By: martijn
Date: 2006-09-10
Time: 13:53
|
Re: Another question/problem
I got some updated code:
function BBCode($FieldName,&$CurrVal) {
//Borrowed the first line of code below from phpBB to make the [url] code work correctly
$CurrVal = preg_replace('#\[url=([\w]+?://[\w\#$%&~/.\-;:=,?@\[\]+]*?)\]([^?\n\r\t].*?)\[/url\]#is', '<a href="$1">$2</a>', $CurrVal);
$CurrVal = preg_replace('/(?:\[[e]?mail=([^\]\s]+)\]([^\[\]]+)\[\/[e]?mail\])/i', '<a href="mailto:\\1">\\2</a>', $CurrVal);
$CurrVal = preg_replace('/(?:\[img=([^\]\s]+)\]([^\[\]]+)\[\/img\])/i', '<img src="\\1" title="\\2" alt="\\2" style="border:0;" />', $CurrVal);
$CurrVal = preg_replace('/(?:\[(?:b|strong)\]([^\[\]]+)\[\/(?:b|strong)\])/i', '<strong>\\1</strong>', $CurrVal);
$CurrVal = preg_replace('/(?:\[(?:i|em)\]([^\[\]]+)\[\/(?:i|em)\])/i', '<em>\\1</em>', $CurrVal);
$CurrVal = preg_replace('/(?:\[u\]([^\[\]]+)\[\/u\])/i', '<span style="text-decoration:underline;">\\1</span>', $CurrVal);
$CurrVal = preg_replace('/(?:\[(?:s|strike)\]([^\[\]]+)\[\/(?:s|strike)\])/i', '<span style="text-decoration:line-through;">\\1</span>', $CurrVal); // added [s] functionality, not tested!
$CurrVal = preg_replace('/(?:\[img\]([^\[\]\s]+)\[\/img\])/i', '<img src="\\1" alt="" style="border:0;" />', $CurrVal);
$CurrVal = preg_replace('/(?:\[url\]([^\[\]\s]+)\[\/url\])/i', '<a href="\\1">\\1</a>', $CurrVal);
$CurrVal = preg_replace('/(?:\[[e]?mail\]([^\[\]\s]+)\[\/[e]?mail\])/i', '<a href="mailto:\\1">\\1</a>', $CurrVal);
$CurrVal = preg_replace('/(?:\[(center|left|right|justify)\]([^\[\]]+)\[\/(center|left|right|justify)\])/i', '<div style="text-align:\\1">\\2</div>', $CurrVal); // not tested!
$CurrVal = str_replace(array("\r\n", "\n", "\r"), '<br />', $CurrVal);
//Emoticon parsing
$CurrVal = str_replace(':)', '<img src="/images/emoticons/icon_smile.gif" alt=":)" />', $CurrVal);
$CurrVal = str_replace(':(', '<img src="/images/emoticons/icon_sad.gif" alt=":(" />', $CurrVal);
$CurrVal = str_replace(':D', '<img src="/images/emoticons/icon_biggrin.gif" alt=":D" />', $CurrVal);
$CurrVal = str_replace(';)', '<img src="/images/emoticons/icon_wink.gif" alt=";)" />', $CurrVal);
$CurrVal = str_replace(':o', '<img src="/images/emoticons/icon_ohmy.gif" alt=":o" />', $CurrVal);
}
|
|
|
Posting in progress.
Please wait...
|