PHP Hacks Free Open Book

PHP Hacks

Previous Page
Next Page

Hack 52. Support Wiki Text

Make it easier for your customers to enter styled text into your application by supporting the Wiki syntax.

A new form of content management system for the Web, Wikis are a collection of pages, each titled with a WikiWord, which is a set of two or more capitalized words joined together without spaces. The ease with which you can install and update Wikis has made them extremely popular both on intranets and on the Internet. Perhaps the most famous Wiki is Wikipedia (http://www.wikipedia.org/). This is an encyclopedia on the Web that anyone can contribute content to by using just their web browser.

Another reason wikis are so popular is that formatting a Wiki page is a lot easier than writing the equivalent HTML code. For example, you specify a paragraph break by just typing two returnsthere is no need to add p tags. In fact, most of the time tags aren't used at all. For example, you create a bulleted list by putting an asterisk at the start of each line; this is far easier than using the equivalent ul and li tags. This hack demonstrates using the wiki-formatting PEAR module in a PHP application.

6.3.1. The Code

Save the code in Example 6-3 as index.php.

Example 6-3. The page that allows you to edit Wiki text
<html>
<body>
<form method="post" action="render.php">
<textarea name="text" cols="80" rows="20">
+ Header Level 1

Here's a paragraph and a link to AnotherPage.</para>

* list item 1
* list item 2

Link to a NewPage like this.
</textarea><br/>
<input type="submit" />
</form>
</body>
</html>

Then save the code in Example 6-4 as render.php.

Example 6-4. The PHP that renders the Wiki text
<html>
<body>
<?php
// Include the Wiki Text Pear library
require_once( "Text/Wiki.php" );

// Create the Wiki object
$wiki = new Text_Wiki();

// Render the text field sent to us in the form
echo( $wiki->transform( $_POST["text" ], 'Xhtml' ) );
?>
</body>
</html>

6.3.2. Running the Hack

This code requires the Text_Wiki PEAR module [Hack #2]. After installing the module and creating the PHP files, navigate the browser to the index.php page shown in Figure 6-3.

Type some Wiki text into the form and click the Submit button. With the text shown in the example, the output looks like Figure 6-4.

Figure 6-3. The text input page for the Wiki text renderer


Figure 6-4. The rendered Wiki text


A complete list of the wiki formatting rules is on the Text_Wiki PEAR component home page at http://wiki.ciaweb.net/yawiki/index.php?area=Text_Wiki&page=WikiRules.


6.3.3. Hacking the Hack

The Text_Wiki module is very well architected and written. New text formatting rules can be added, and the default formatting rules can be enabled and disabled to suit the application. The enableRule() and disableRule() methods enable and disable the built-in text formatting rules. The addRule() method adds new rules to the formatting engine.

Previous Page
Next Page
Index: [SYMBOL][A][B][C][D][E][F][G][H][I][J][K][L][M][N][O][P][Q][R][S][T][U][V][W][X][Y][Z]


     Main Menu
PHP Hacks
Table of Contents
Copyright
Credits
Preface
Chapter 1.  Installation and Basics
Chapter 2.  Web Design
Chapter 3.  DHTML
Chapter 4.  Graphics
Chapter 5.  Databases and XML
Chapter 6.  Application Design
Section 6.1.  Hacks 5166: Introduction
Hack 51. Create Modular Interfaces
Hack 52. Support Wiki Text
Hack 53. Turn Any Object into an Array
Hack 54. Create XML the Right Way
Hack 55. Fix the Double Submit Problem
Hack 56. Create User-Customizable Reports
Hack 57. Create a Login System
Hack 58. Apply Security by Role
Hack 59. Migrate to MD5 Passwords
Hack 60. Make Usable URLs with mod_rewrite
Hack 61. Build an Ad Redirector
Hack 62. Add a Buy Now Button
Hack 63. Find Out Where Your Guests Are Coming From
Hack 64. Import Information from vCards
Hack 65. Create vCard Files from Your Application's Data
Hack 66. Create a Shopping Cart
Chapter 7.  Patterns
Chapter 8.  Testing
Chapter 9.  Alternative UIs
Chapter 10.  Fun Stuff
Colophon
Index


More Books
PHP Hacks
Processing Xml With Java - A Guide To Sax, Dom, Jdom, Jaxp, And Trax
The Koran (Holy Qur'an)
Macromedia Flash 8 Bible
Search Engine Optimization for Dummies
YouTube Traffic
PHP 5 for Dummies
Harry Potter and The Chamber of Secrets
Harry Potter and the Sorcerer's Stone
The Pilgrim's Progress
Wireless Hacks
Flash Hacks. 100 Industrial-Strength Tips & Tools
PayPal Hacks. 100 Industrial-Strength Tips and Tools
Amazon Hacks
Pdf Hacks
The Da Vinci Code
Google Hacks
The Holy Bible
Windows XP For Dummies
Harry Potter and the Half-Blood Prince
Seo Book
Upgrading and Repairing Networks
Macromedia Dreamweaver 8 UNLEASHED
Windows XP Annoyances
Windows XP Hacks
Microsoft Windows XP Power Toolkit
Teach Yourself MS Office In 24Hours
iPod & iTunes Missing Manual
PC Hacks 100 Industrial-Strength Tips and Tools
PC Overclocking, Optimization, and Tuning - 2th Edition
PC Hardware In A Nutshell 3rd Edition
PC Hardware in a Nutshell, 2nd Edition
Upgrading and Repairing PCs
Google for Dummies
MySQL Cookbook
Teach Yourself Macromedia Flash 8 In 24 Hours
PHP CookBook
Sams Teach Yourself JavaScript in 24 Hours
PHP5 Manual
Free Games Paper Airplanes
500 Juegos Gratis 500 Giochi Gratis 500 Jeux Gratuits 500 Jogos Gratis 500 Kostenlose Spiele