Wrox Programmer Forums
Go Back   Wrox Programmer Forums > PHP/MySQL > PHP How-To
|
PHP How-To Post your "How do I do this with PHP?" questions here.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the PHP How-To section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old July 21st, 2004, 12:39 PM
Registered User
 
Join Date: Jul 2004
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Default Content formatting toolbar

Does anyone know of a link to a script or tutorial I can use to add text formatting buttons like the ones used on this forum to a content input application page? Currently anything I input is rinsed through the htmlspecialchars() function where any HTML formating is neutralized into a string.

Thanks for your help.

 
Old July 21st, 2004, 01:51 PM
Friend of Wrox
 
Join Date: Nov 2003
Posts: 1,285
Thanks: 0
Thanked 2 Times in 2 Posts
Default

Do you want the buttons that dynamically add the tags, or do you want the server code that makes the tags work?

I'm assuming the server code, because you're asking in the PHP section....

This is what I do: allow users to use tags like this forum does ([i],[b], etc.) and then use this code:

Code:
//get what the user is trying to post
$message = $_POST['message'];

//convert line breaks
$message = nl2br($message); 

$arr_html = Array("<",">");

$arr_conv_html = Array("&lt;","&gt;");

//replace html with escaped version
$message = str_replace($arr_html,$arr_conv_html,$message);

$bracketcode = Array("[b]","[i]","[u]");

$HTMLcode = Array("<b>","<i>","[u]");

//replace bracket code with html
$message = str_replace($bracketcode,$HTMLcode,$message);

//insert the message into the database here
HTH,

Snib

<><
 
Old August 12th, 2004, 02:43 PM
Registered User
 
Join Date: Jul 2004
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Default

These forums are quite helpful. Now for some more of that help... :)

QUESTION 1 :)

I've setup the BBcode functions to format my text without inputting harmful values into the database. Now I'd like some help with the button toolbar that dynamically adds the BB tags into the textarea. I'd like my article compose toolbar to be very similar to the Format section of the one used on these forums. It works great!

Here's a quick summary of the BB system I've pieced together (from Chapter 12 and 15 of Beginning PHP, Apache, MySQL):

1) I created the 'forum_bbcode' table from Chap.15 in my database.

2) I added the function below to my 'outputfunctions.php' file.

function bbcode($data) {
  $sql = "SELECT * FROM forum_bbcode";
  $result = mysql_query($sql);
  if (mysql_num_rows($result) > 0) {
    while($row = mysql_fetch_array($result)) {
      $bbcode['tpl'][] =
        "§" . html_entity_decode($row['template'],ENT_QUOTES). "§i";
      $bbcode['rep'][] =
        html_entity_decode($row['replacement'],ENT_QUOTES);
    }
    $data1 = preg_replace($bbcode['tpl'],$bbcode['rep'],$data);
    $count = 1;
    while (($data1 != $data) and ($count < 4)) {
      $count++;
      $data = $data1;
      $data1 = preg_replace($bbcode['tpl'],$bbcode['rep'],$data);
    }
  }

  return $data;
}

3) I changed the article story output code to: echo bbcode(nl2br(htmlspecialchars($row['body']))); from: echo nl2br(htmlspecialchars($row['body'])); , where applicable.

4) I also manually added: \[b\]([^[]+?)\[/b\] and <b>$1</b> to the table so that it replaces the BBcode with HTML. The result is a working BBcode setup!


QUESTION 2 :D

I'd also like to know where I can find more PCRE expressions like the bold one above and the ones in p. 547 of the textbook.

In your debt.

 
Old August 12th, 2004, 04:15 PM
richard.york's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 1,706
Thanks: 0
Thanked 6 Times in 6 Posts
Default

I've recommended it before, and here it is again ;)...

I personally like the HTML_BBCodeParser package offered in PEAR. I was fooling around with this about a week ago and I liked how it made sure that all of the tags were closed and nested correctly, in other words it didn't just replace the BBCode markup, it made sure it was properly nested and all of that as well. I found it also was rather easy to fork with my own custom BB code syntax. In fact I modified the package rather painlessly to produce markup that uses all CSS, where appropriate, is accessible and is XHTML compliant.

If you want that package go to:
http://pear.php.net/HTML_BBCodeParser

It saved me the trouble of having to come up with complex regular expressions on my own. If you'd like to have a peek at the source code of my modified version of that package and the JS that I use to apply the BBCode, drop me an email and I'll send you the source code. In fact the JS that I used was based largely on the Snitz JS used in this forum, it was very easy to optimize and customize. I just copied their libraries and made some modifications to reduce the amount of code necessary and wrapped it in PHP so that it is abstracted and easily applied to any textarea field.

Regards,
Rich

--
[http://www.smilingsouls.net]
[http://pear.php.net/Mail_IMAP] A PHP/C-Client/PEAR solution for webmail





Similar Threads
Thread Thread Starter Forum Replies Last Post
Choosing content depending on content of other ele dsekar_nat XSLT 1 February 27th, 2006 05:58 AM
ToolBar Help reyboy VB.NET 2002/2003 Basics 2 April 19th, 2005 08:35 PM
About the toolbar momowu0701 BOOK: Beginning VB.NET 2nd Edition/Beginning VB.NET 2003 0 March 1st, 2005 09:23 AM
Toolbar amantona Crystal Reports 0 May 12th, 2004 09:41 AM
Toolbar sangata VS.NET 2002/2003 2 April 13th, 2004 05:14 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.